1 /***
2 *
3 * Copyright 2004 Hiram Chirino
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activemq.store.jdbc;
19
20 import java.sql.Connection;
21 import java.sql.SQLException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import javax.jms.JMSException;
26 import javax.transaction.xa.XAException;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.codehaus.activemq.message.ActiveMQXid;
31 import org.codehaus.activemq.message.WireFormat;
32 import org.codehaus.activemq.service.Transaction;
33 import org.codehaus.activemq.service.TransactionManager;
34 import org.codehaus.activemq.store.PreparedTransactionStore;
35
36 /***
37 * @version $Revision: 1.1 $
38 */
39 public class JDBCPreparedTransactionStore implements PreparedTransactionStore {
40 private static final Log log = LogFactory.getLog(JDBCMessageStore.class);
41
42 private final WireFormat wireFormat;
43 private final JDBCAdapter adapter;
44
45 public JDBCPreparedTransactionStore(JDBCAdapter adapter, WireFormat wireFormat) {
46 this.adapter = adapter;
47 this.wireFormat = wireFormat;
48 }
49
50 public ActiveMQXid[] getXids() throws XAException {
51
52
53 List list = new ArrayList();
54
55 Connection c = TransactionContext.getConnection();
56 try {
57 adapter.doGetXids(c, list);
58 } catch (SQLException e) {
59 log.error("Failed to recover prepared transaction log: " + e, e);
60 throw new XAException("Failed to recover container. Reason: " + e);
61 }
62
63 ActiveMQXid[] answer = new ActiveMQXid[list.size()];
64 list.toArray(answer);
65 return answer;
66 }
67
68 public void remove(ActiveMQXid xid) throws XAException {
69
70
71 try {
72 Connection c = TransactionContext.getConnection();
73 adapter.doRemoveXid(c, xid);
74 } catch (SQLException e) {
75 throw new XAException("Failed to remove prepared transaction: " + xid + ". Reason: " + e);
76 }
77
78 }
79
80 public void put(ActiveMQXid xid, Transaction transaction) throws XAException {
81
82
83 String id = xid.toLocalTransactionId();
84 byte data[];
85 try {
86 data = xid.toBytes();
87 } catch (Exception e) {
88 throw new XAException("Failed to store prepared transaction: " + xid + ". Reason: " + e);
89 }
90
91
92 try {
93 Connection c = TransactionContext.getConnection();
94 adapter.doAddXid(c, xid, data);
95 } catch (SQLException e) {
96 throw new XAException("Failed to store prepared transaction: " + xid + ". Reason: " + e);
97 }
98 }
99
100 public void loadPreparedTransactions(TransactionManager transactionManager) throws XAException {
101
102 try {
103 Connection c = TransactionContext.getConnection();
104 adapter.doLoadPreparedTransactions(c, transactionManager);
105 } catch (SQLException e) {
106 log.error("Failed to recover prepared transaction log: " + e, e);
107 throw new XAException("Failed to recover prepared transaction log. Reason: " + e);
108 }
109
110 }
111
112 public void start() throws JMSException {
113 }
114
115 public synchronized void stop() throws JMSException {
116 }
117
118 }