View Javadoc

1   /*
2    * $Id: JmsTransaction.java 7976 2007-08-21 14:26:13Z 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.providers.jms;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.providers.jms.i18n.JmsMessages;
15  import org.mule.transaction.AbstractSingleResourceTransaction;
16  import org.mule.transaction.IllegalTransactionStateException;
17  import org.mule.umo.TransactionException;
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          try
63          {
64              ((Session)resource).commit();
65          }
66          catch (JMSException e)
67          {
68              throw new TransactionException(CoreMessages.transactionCommitFailed(), e);
69          }
70      }
71  
72      protected void doRollback() throws TransactionException
73      {
74          try
75          {
76              ((Session)resource).rollback();
77          }
78          catch (JMSException e)
79          {
80              throw new TransactionException(CoreMessages.transactionRollbackFailed(), e);
81          }
82      }
83  }