View Javadoc

1   /*
2    * $Id: SpringTransactionFactory.java 8977 2007-10-08 11:48:55Z romikk $
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.extras.spring.transaction;
12  
13  import org.mule.transaction.AbstractSingleResourceTransaction;
14  import org.mule.umo.TransactionException;
15  import org.mule.umo.UMOTransaction;
16  import org.mule.umo.UMOTransactionFactory;
17  
18  import org.springframework.jdbc.datasource.ConnectionHolder;
19  import org.springframework.jms.connection.JmsResourceHolder;
20  import org.springframework.transaction.PlatformTransactionManager;
21  import org.springframework.transaction.TransactionStatus;
22  import org.springframework.transaction.support.TransactionSynchronizationManager;
23  
24  /**
25   * TODO: document this class
26   */
27  public class SpringTransactionFactory implements UMOTransactionFactory
28  {
29  
30      private PlatformTransactionManager manager;
31  
32      public SpringTransactionFactory()
33      {
34          super();
35      }
36  
37      public UMOTransaction beginTransaction() throws TransactionException
38      {
39          UMOTransaction tx = new SpringTransaction();
40          tx.begin();
41          return tx;
42      }
43  
44      public boolean isTransacted()
45      {
46          return true;
47      }
48  
49      /**
50       * @return Returns the manager.
51       */
52      synchronized public PlatformTransactionManager getManager()
53      {
54          return manager;
55      }
56  
57      /**
58       * @param manager The manager to set.
59       */
60      synchronized public void setManager(PlatformTransactionManager manager)
61      {
62          this.manager = manager;
63      }
64  
65      /**
66       * TODO: document this class
67       */
68      public class SpringTransaction extends AbstractSingleResourceTransaction
69      {
70          protected final TransactionStatus status;
71  
72          public SpringTransaction()
73          {
74              status = manager.getTransaction(null);
75          }
76  
77          protected void doBegin() throws TransactionException
78          {
79              // nothing to do
80          }
81  
82          protected void doCommit() throws TransactionException
83          {
84              manager.commit(status);
85          }
86  
87          protected void doRollback() throws TransactionException
88          {
89              manager.rollback(status);
90          }
91  
92          public Object getResource(Object key)
93          {
94              Object res = TransactionSynchronizationManager.getResource(key);
95              if (res != null)
96              {
97                  if (!(res instanceof ConnectionHolder))
98                  {
99                      if (res instanceof JmsResourceHolder)
100                     {
101                         return ((JmsResourceHolder)res).getConnection();
102                     }
103                 }
104                 else
105                 {
106                     return ((ConnectionHolder)res).getConnection();
107                 }
108             }
109             return res;
110         }
111 
112         public boolean hasResource(Object key)
113         {
114             return getResource(key) != null;
115         }
116 
117         public void bindResource(Object key, Object resource) throws TransactionException
118         {
119             throw new UnsupportedOperationException();
120         }
121 
122         public void setRollbackOnly()
123         {
124             super.setRollbackOnly();
125             status.setRollbackOnly();
126         }
127 
128     }
129 
130 }