View Javadoc

    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.2 $
    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  	private final JDBCPersistenceAdapter persistenceAdapter;
    45
    46      public JDBCPreparedTransactionStore(JDBCPersistenceAdapter persistenceAdapter, JDBCAdapter adapter, WireFormat wireFormat) {
    47          this.persistenceAdapter = persistenceAdapter;
    48  		this.adapter = adapter;
    49          this.wireFormat = wireFormat;
    50      }
    51
    52      public ActiveMQXid[] getXids() throws XAException {
    53
    54          // Pull xids out of the DB and put them in here.
    55          List list = new ArrayList();
    56
    57          Connection c = null;
    58          try {
    59              c = persistenceAdapter.getConnection();
    60              adapter.doGetXids(c, list);
    61          } catch (SQLException e) {
    62              log.error("Failed to recover prepared transaction log: " + e, e);
    63              throw new XAException("Failed to recover container. Reason: " + e);
    64          } finally {
    65          	persistenceAdapter.returnConnection(c);
    66          }
    67
    68          ActiveMQXid[] answer = new ActiveMQXid[list.size()];
    69          list.toArray(answer);
    70          return answer;
    71      }
    72
    73      public void remove(ActiveMQXid xid) throws XAException {
    74
    75          // Get a connection and remove the message from the DB
    76          Connection c = null;
    77          try {
    78              c = persistenceAdapter.getConnection();
    79              adapter.doRemoveXid(c, xid);
    80          } catch (SQLException e) {
    81              throw new XAException("Failed to remove prepared transaction: " + xid + ". Reason: " + e);
    82          } finally {
    83          	persistenceAdapter.returnConnection(c);
    84          }
    85
    86      }
    87
    88      public void put(ActiveMQXid xid, Transaction transaction) throws XAException {
    89
    90          // Serialize the Message..
    91          String id = xid.toLocalTransactionId();
    92          byte data[];
    93          try {
    94              data = xid.toBytes();
    95          } catch (Exception e) {
    96              throw new XAException("Failed to store prepared transaction: " + xid + ". Reason: " + e);
    97          }
    98
    99          // Get a connection and insert the message into the DB.
    100         Connection c = null;
    101         try {
    102             c = persistenceAdapter.getConnection();
    103             adapter.doAddXid(c, xid, data);
    104         } catch (SQLException e) {
    105             throw new XAException("Failed to store prepared transaction: " + xid + ". Reason: " + e);
    106         } finally {
    107         	persistenceAdapter.returnConnection(c);
    108         }
    109     }
    110
    111     public void loadPreparedTransactions(TransactionManager transactionManager) throws XAException {
    112
    113         Connection c = null;
    114         try {
    115             c = persistenceAdapter.getConnection();
    116             adapter.doLoadPreparedTransactions(c, transactionManager);
    117         } catch (SQLException e) {
    118             log.error("Failed to recover prepared transaction log: " + e, e);
    119             throw new XAException("Failed to recover prepared transaction log. Reason: " + e);
    120         } finally {
    121         	persistenceAdapter.returnConnection(c);
    122         }
    123
    124     }
    125
    126     public void start() throws JMSException {
    127     }
    128
    129     public synchronized void stop() throws JMSException {
    130     }
    131
    132 }