View Javadoc

1   /*
2    * $Id: JmsTransaction.java 12181 2008-06-26 20:05:55Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.jms;
12  
13  import org.mule.api.transaction.TransactionException;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.transaction.AbstractSingleResourceTransaction;
16  import org.mule.transaction.IllegalTransactionStateException;
17  import org.mule.transport.jms.i18n.JmsMessages;
18  
19  import javax.jms.Connection;
20  import javax.jms.JMSException;
21  import javax.jms.Session;
22  
23  /**
24   * <code>JmsTransaction</code> is a wrapper for a JMS local transaction. This
25   * object holds the JMS session and controls when the transaction is committed or
26   * rolled back.
27   */
28  public class JmsTransaction extends AbstractSingleResourceTransaction
29  {
30  
31      public void bindResource(Object key, Object resource) throws TransactionException
32      {
33          if (!(key instanceof Connection) || !(resource instanceof Session))
34          {
35              throw new IllegalTransactionStateException(
36                  CoreMessages.transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session"));
37          }
38  
39          Session session = (Session)resource;
40          try
41          {
42              if (!session.getTransacted())
43              {
44                  throw new IllegalTransactionStateException(JmsMessages.sessionShouldBeTransacted());
45              }
46          }
47          catch (JMSException e)
48          {
49              throw new IllegalTransactionStateException(CoreMessages.transactionCannotReadState(), e);
50          }
51  
52          super.bindResource(key, resource);
53      }
54  
55      protected void doBegin() throws TransactionException
56      {
57          // do nothing
58      }
59  
60      protected void doCommit() throws TransactionException
61      {
62          if (resource == null)
63          {
64              logger.warn(CoreMessages.noBindingResource());
65              return;
66          }
67          
68          try
69          {
70              ((Session)resource).commit();
71          }
72          catch (JMSException e)
73          {
74              throw new TransactionException(CoreMessages.transactionCommitFailed(), e);
75          }
76      }
77  
78      protected void doRollback() throws TransactionException
79      {
80          if (resource == null)
81          {
82              logger.warn(CoreMessages.noBindingResource());
83              return;
84          }
85  
86          try
87          {
88              ((Session)resource).rollback();
89          }
90          catch (JMSException e)
91          {
92              throw new TransactionException(CoreMessages.transactionRollbackFailed(), e);
93          }    
94      }
95      
96  }