View Javadoc

1   /*
2    * $Id: JmsTransaction.java 9429 2007-10-29 11:09:10Z tcarlson $
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.providers.jms;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.config.i18n.MessageFactory;
15  import org.mule.providers.jms.i18n.JmsMessages;
16  import org.mule.transaction.AbstractSingleResourceTransaction;
17  import org.mule.transaction.IllegalTransactionStateException;
18  import org.mule.umo.TransactionException;
19  
20  import javax.jms.Connection;
21  import javax.jms.JMSException;
22  import javax.jms.Session;
23  
24  /**
25   * <code>JmsTransaction</code> is a wrapper for a JMS local transaction. This
26   * object holds the JMS session and controls when the transaction is committed or
27   * rolled back.
28   */
29  public class JmsTransaction extends AbstractSingleResourceTransaction
30  {
31  
32      public void bindResource(Object key, Object resource) throws TransactionException
33      {
34          if (!(key instanceof Connection) || !(resource instanceof Session))
35          {
36              throw new IllegalTransactionStateException(
37                  CoreMessages.transactionCanOnlyBindToResources("javax.jms.Connection/javax.jms.Session"));
38          }
39  
40          Session session = (Session)resource;
41          try
42          {
43              if (!session.getTransacted())
44              {
45                  throw new IllegalTransactionStateException(JmsMessages.sessionShouldBeTransacted());
46              }
47          }
48          catch (JMSException e)
49          {
50              throw new IllegalTransactionStateException(CoreMessages.transactionCannotReadState(), e);
51          }
52  
53          super.bindResource(key, resource);
54      }
55  
56      protected void doBegin() throws TransactionException
57      {
58          // do nothing
59      }
60  
61      protected void doCommit() throws TransactionException
62      {
63          try
64          {
65              ((Session)resource).commit();
66          }
67          catch (JMSException e)
68          {
69              throw new TransactionException(CoreMessages.transactionCommitFailed(), e);
70          }
71      }
72  
73      protected void doRollback() throws TransactionException
74      {
75          if (resource != null)
76          {
77              try
78              {
79                  ((Session)resource).rollback();
80              }
81              catch (JMSException e)
82              {
83                  throw new TransactionException(CoreMessages.transactionRollbackFailed(), e);
84              }
85          }
86          else 
87          {
88              throw new TransactionException(MessageFactory.createStaticMessage("No resource has been bound to this transaction"));
89          }
90      }
91  }