KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > spring > transaction > SpringTransactionFactory


1 /*
2  * $Id: SpringTransactionFactory.java 4259 2006-12-14 03:12:07Z aperepel $
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 MuleSource MPL
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 import org.springframework.jdbc.datasource.ConnectionHolder;
18 import org.springframework.jms.connection.JmsResourceHolder;
19 import org.springframework.transaction.PlatformTransactionManager;
20 import org.springframework.transaction.TransactionStatus;
21 import org.springframework.transaction.support.TransactionSynchronizationManager;
22
23 /**
24  * TODO: document this class
25  */

26 public class SpringTransactionFactory implements UMOTransactionFactory
27 {
28
29     private PlatformTransactionManager manager;
30
31     public SpringTransactionFactory()
32     {
33         super();
34     }
35
36     public UMOTransaction beginTransaction() throws TransactionException
37     {
38         return new SpringTransaction();
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()
70         {
71             status = manager.getTransaction(null);
72         }
73
74         protected void doBegin() throws TransactionException
75         {
76             // nothing to do
77
}
78
79         protected void doCommit() throws TransactionException
80         {
81             manager.commit(status);
82         }
83
84         protected void doRollback() throws TransactionException
85         {
86             manager.rollback(status);
87         }
88
89         public Object JavaDoc getResource(Object JavaDoc key)
90         {
91             Object JavaDoc res = TransactionSynchronizationManager.getResource(key);
92             if (res != null)
93             {
94                 if (!(res instanceof ConnectionHolder))
95                 {
96                     if (res instanceof JmsResourceHolder)
97                     {
98                         return ((JmsResourceHolder)res).getConnection();
99                     }
100                 }
101                 else
102                 {
103                     return ((ConnectionHolder)res).getConnection();
104                 }
105             }
106             return res;
107         }
108
109         public boolean hasResource(Object JavaDoc key)
110         {
111             return getResource(key) != null;
112         }
113
114         public void bindResource(Object JavaDoc key, Object JavaDoc resource) throws TransactionException
115         {
116             throw new UnsupportedOperationException JavaDoc();
117         }
118
119         public void setRollbackOnly()
120         {
121             super.setRollbackOnly();
122             status.setRollbackOnly();
123         }
124
125     }
126
127 }
128
Popular Tags