1
2
3
4
5
6
7
8
9
10
11 package org.mule.transaction;
12
13 import org.mule.config.i18n.CoreMessages;
14 import org.mule.umo.UMOTransaction;
15 import org.mule.umo.UMOTransactionConfig;
16
17 import java.beans.ExceptionListener;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 public class TransactionTemplate
23 {
24 private static final Log logger = LogFactory.getLog(TransactionTemplate.class);
25
26 private final UMOTransactionConfig config;
27 private final ExceptionListener exceptionListener;
28
29 public TransactionTemplate(UMOTransactionConfig config, ExceptionListener listener)
30 {
31 this.config = config;
32 exceptionListener = listener;
33 }
34
35 public Object execute(TransactionCallback callback) throws Exception
36 {
37 if (config == null)
38 {
39 return callback.doInTransaction();
40 }
41 else
42 {
43 byte action = config.getAction();
44 UMOTransaction tx = TransactionCoordination.getInstance().getTransaction();
45
46 if (action == UMOTransactionConfig.ACTION_NONE && tx != null)
47 {
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 throw new IllegalTransactionStateException(
66 CoreMessages.transactionAvailableButActionIs("None"));
67 }
68 else if (action == UMOTransactionConfig.ACTION_ALWAYS_BEGIN && tx != null)
69 {
70 throw new IllegalTransactionStateException(
71 CoreMessages.transactionAvailableButActionIs("Always Begin"));
72 }
73 else if (action == UMOTransactionConfig.ACTION_ALWAYS_JOIN && tx == null)
74 {
75 throw new IllegalTransactionStateException(
76 CoreMessages.transactionNotAvailableButActionIs("Always Join"));
77 }
78
79 if (action == UMOTransactionConfig.ACTION_ALWAYS_BEGIN
80 || (action == UMOTransactionConfig.ACTION_BEGIN_OR_JOIN && tx == null))
81 {
82 logger.debug("Beginning transaction");
83 tx = config.getFactory().beginTransaction();
84 logger.debug("Transaction successfully started");
85 }
86 else
87 {
88 tx = null;
89 }
90 try
91 {
92 Object result = callback.doInTransaction();
93 if (tx != null)
94 {
95 if (tx.isRollbackOnly())
96 {
97 logger.debug("Transaction is marked for rollback");
98 tx.rollback();
99 }
100 else
101 {
102 logger.debug("Committing transaction");
103 tx.commit();
104 }
105 }
106 return result;
107 }
108 catch (Exception e)
109 {
110 if (exceptionListener != null)
111 {
112 logger
113 .info("Exception Caught in Transaction template. Handing off to exception handler: "
114 + exceptionListener);
115 exceptionListener.exceptionThrown(e);
116 }
117 else
118 {
119 logger
120 .info("Exception Caught in Transaction template without any exception listeners defined, exception is rethrown.");
121 if (tx != null)
122 {
123 tx.setRollbackOnly();
124 }
125 }
126 if (tx != null)
127 {
128
129
130
131
132
133
134
135 if (tx.isRollbackOnly())
136 {
137 logger.debug("Exception caught: rollback transaction", e);
138 tx.rollback();
139 }
140 else
141 {
142 tx.commit();
143 }
144 }
145
146 if (exceptionListener != null)
147 {
148 return null;
149 }
150 else
151 {
152 throw e;
153 }
154 }
155 catch (Error e)
156 {
157 if (tx != null)
158 {
159
160 logger.info("Error caught: rollback transaction", e);
161 tx.rollback();
162 }
163 throw e;
164 }
165 }
166 }
167
168 }