View Javadoc

1   /*
2    * $Id: SpringTransactionFactory.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.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          return new SpringTransaction();
40      }
41  
42      public boolean isTransacted()
43      {
44          return true;
45      }
46  
47      /**
48       * @return Returns the manager.
49       */
50      synchronized public PlatformTransactionManager getManager()
51      {
52          return manager;
53      }
54  
55      /**
56       * @param manager The manager to set.
57       */
58      synchronized public void setManager(PlatformTransactionManager manager)
59      {
60          this.manager = manager;
61      }
62  
63      /**
64       * TODO: document this class
65       */
66      public class SpringTransaction extends AbstractSingleResourceTransaction
67      {
68          protected final TransactionStatus status;
69  
70          public SpringTransaction()
71          {
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 }