1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.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.config.i18n.CoreMessages; |
13 | |
import org.mule.context.notification.TransactionNotification; |
14 | |
import org.mule.util.UUID; |
15 | |
|
16 | |
import java.text.MessageFormat; |
17 | |
|
18 | |
import org.apache.commons.logging.Log; |
19 | |
import org.apache.commons.logging.LogFactory; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public abstract class AbstractTransaction implements Transaction |
25 | |
{ |
26 | |
|
27 | 0 | protected final transient Log logger = LogFactory.getLog(getClass()); |
28 | |
|
29 | 0 | protected String id = UUID.getUUID(); |
30 | |
|
31 | |
protected MuleContext muleContext; |
32 | |
|
33 | |
protected AbstractTransaction(MuleContext muleContext) |
34 | 0 | { |
35 | 0 | this.muleContext = muleContext; |
36 | 0 | } |
37 | |
|
38 | |
public boolean isRollbackOnly() throws TransactionException |
39 | |
{ |
40 | 0 | return getStatus() == STATUS_MARKED_ROLLBACK; |
41 | |
} |
42 | |
|
43 | |
public boolean isBegun() throws TransactionException |
44 | |
{ |
45 | 0 | int status = getStatus(); |
46 | 0 | return status != STATUS_NO_TRANSACTION && status != STATUS_UNKNOWN; |
47 | |
} |
48 | |
|
49 | |
public boolean isRolledBack() throws TransactionException |
50 | |
{ |
51 | 0 | return getStatus() == STATUS_ROLLEDBACK; |
52 | |
} |
53 | |
|
54 | |
public boolean isCommitted() throws TransactionException |
55 | |
{ |
56 | 0 | return getStatus() == STATUS_COMMITTED; |
57 | |
} |
58 | |
|
59 | |
public void begin() throws TransactionException |
60 | |
{ |
61 | 0 | logger.debug("Beginning transaction"); |
62 | 0 | doBegin(); |
63 | 0 | TransactionCoordination.getInstance().bindTransaction(this); |
64 | 0 | fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_BEGAN)); |
65 | 0 | } |
66 | |
|
67 | |
public void commit() throws TransactionException |
68 | |
{ |
69 | |
try |
70 | |
{ |
71 | 0 | logger.debug("Committing transaction " + this); |
72 | |
|
73 | 0 | if (isRollbackOnly()) |
74 | |
{ |
75 | 0 | throw new IllegalTransactionStateException(CoreMessages.transactionMarkedForRollback()); |
76 | |
} |
77 | |
|
78 | 0 | doCommit(); |
79 | 0 | fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_COMMITTED)); |
80 | |
} |
81 | |
finally |
82 | |
{ |
83 | 0 | TransactionCoordination.getInstance().unbindTransaction(this); |
84 | 0 | } |
85 | 0 | } |
86 | |
|
87 | |
public void rollback() throws TransactionException |
88 | |
{ |
89 | |
try |
90 | |
{ |
91 | 0 | logger.debug("Rolling back transaction"); |
92 | 0 | setRollbackOnly(); |
93 | 0 | doRollback(); |
94 | 0 | fireNotification(new TransactionNotification(this, TransactionNotification.TRANSACTION_ROLLEDBACK)); |
95 | |
} |
96 | |
finally |
97 | |
{ |
98 | 0 | unbindTransaction(); |
99 | 0 | } |
100 | 0 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
protected void unbindTransaction() throws TransactionException |
106 | |
{ |
107 | 0 | TransactionCoordination.getInstance().unbindTransaction(this); |
108 | 0 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
protected abstract void doBegin() throws TransactionException; |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
protected abstract void doCommit() throws TransactionException; |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
protected abstract void doRollback() throws TransactionException; |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
protected void fireNotification(TransactionNotification notification) |
138 | |
{ |
139 | |
|
140 | 0 | muleContext.fireNotification(notification); |
141 | 0 | } |
142 | |
|
143 | |
public boolean isXA() |
144 | |
{ |
145 | 0 | return false; |
146 | |
} |
147 | |
|
148 | |
public void resume() throws TransactionException |
149 | |
{ |
150 | 0 | throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(this)); |
151 | |
} |
152 | |
|
153 | |
public javax.transaction.Transaction suspend() throws TransactionException |
154 | |
{ |
155 | 0 | throw new IllegalTransactionStateException(CoreMessages.notMuleXaTransaction(this)); |
156 | |
} |
157 | |
|
158 | |
public String getId() |
159 | |
{ |
160 | 0 | return id; |
161 | |
} |
162 | |
|
163 | |
@Override |
164 | |
public String toString() |
165 | |
{ |
166 | |
int status; |
167 | |
try |
168 | |
{ |
169 | 0 | status = getStatus(); |
170 | |
} |
171 | 0 | catch (TransactionException e) |
172 | |
{ |
173 | 0 | status = -1; |
174 | 0 | } |
175 | 0 | return MessageFormat.format("{0}[id={1} , status={2}]", getClass().getName(), id, status); |
176 | |
} |
177 | |
} |