View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.spring.transaction;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.transaction.Transaction;
11  import org.mule.api.transaction.TransactionException;
12  import org.mule.api.transaction.TransactionFactory;
13  import org.mule.transaction.AbstractSingleResourceTransaction;
14  
15  import org.springframework.jdbc.datasource.ConnectionHolder;
16  import org.springframework.jms.connection.JmsResourceHolder;
17  import org.springframework.transaction.PlatformTransactionManager;
18  import org.springframework.transaction.TransactionStatus;
19  import org.springframework.transaction.support.TransactionSynchronizationManager;
20  
21  /**
22   * TODO: document this class
23   */
24  public class SpringTransactionFactory implements TransactionFactory
25  {
26  
27      private PlatformTransactionManager manager;
28  
29      public SpringTransactionFactory()
30      {
31          super();
32      }
33  
34      public Transaction beginTransaction(MuleContext muleContext) throws TransactionException
35      {
36          Transaction tx = new SpringTransaction(muleContext);
37          tx.begin();
38          return tx;
39      }
40  
41      public boolean isTransacted()
42      {
43          return true;
44      }
45  
46      /**
47       * @return Returns the manager.
48       */
49      synchronized public PlatformTransactionManager getManager()
50      {
51          return manager;
52      }
53  
54      /**
55       * @param manager The manager to set.
56       */
57      synchronized public void setManager(PlatformTransactionManager manager)
58      {
59          this.manager = manager;
60      }
61  
62      /**
63       * TODO: document this class
64       */
65      public class SpringTransaction extends AbstractSingleResourceTransaction
66      {
67          protected final TransactionStatus status;
68  
69          public SpringTransaction(MuleContext muleContext)
70          {
71              super(muleContext);
72              status = manager.getTransaction(null);
73          }
74  
75          protected void doBegin() throws TransactionException
76          {
77              // nothing to do
78          }
79  
80          protected void doCommit() throws TransactionException
81          {
82             manager.commit(status);
83          }
84  
85          protected void doRollback() throws TransactionException
86          {
87             manager.rollback(status);
88          }
89  
90          public Object getResource(Object key)
91          {
92              Object res = TransactionSynchronizationManager.getResource(key);
93              if (res != null)
94              {
95                  if (!(res instanceof ConnectionHolder))
96                  {
97                      if (res instanceof JmsResourceHolder)
98                      {
99                          return ((JmsResourceHolder)res).getConnection();
100                     }
101                 }
102                 else
103                 {
104                     return ((ConnectionHolder)res).getConnection();
105                 }
106             }
107             return res;
108         }
109 
110         public boolean hasResource(Object key)
111         {
112             return getResource(key) != null;
113         }
114 
115         public void bindResource(Object key, Object resource) throws TransactionException
116         {
117             throw new UnsupportedOperationException();
118         }
119 
120         public void setRollbackOnly()
121         {
122             super.setRollbackOnly();
123             status.setRollbackOnly();
124         }
125 
126     }
127 
128 }