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.TransactionException; |
15 | |
import org.mule.umo.UMOTransaction; |
16 | |
|
17 | |
import org.apache.commons.logging.Log; |
18 | |
import org.apache.commons.logging.LogFactory; |
19 | |
|
20 | |
public final class TransactionCoordination |
21 | |
{ |
22 | 0 | protected static final Log logger = LogFactory.getLog(TransactionCoordination.class); |
23 | |
|
24 | 0 | private static final TransactionCoordination instance = new TransactionCoordination(); |
25 | |
|
26 | 0 | private static final ThreadLocal transactions = new ThreadLocal(); |
27 | |
|
28 | |
|
29 | 0 | private int txCounter = 0; |
30 | |
|
31 | |
|
32 | |
private TransactionCoordination() |
33 | |
{ |
34 | 0 | super(); |
35 | 0 | } |
36 | |
|
37 | |
public static TransactionCoordination getInstance() |
38 | |
{ |
39 | 0 | return instance; |
40 | |
} |
41 | |
|
42 | |
public UMOTransaction getTransaction() |
43 | |
{ |
44 | 0 | return (UMOTransaction) transactions.get(); |
45 | |
} |
46 | |
|
47 | |
public void unbindTransaction(UMOTransaction transaction) throws TransactionException |
48 | |
{ |
49 | |
try |
50 | |
{ |
51 | 0 | UMOTransaction oldTx = (UMOTransaction) transactions.get(); |
52 | 0 | if (oldTx != null && !oldTx.equals(transaction)) |
53 | |
{ |
54 | 0 | throw new IllegalTransactionStateException(CoreMessages.transactionCannotUnbind()); |
55 | |
} |
56 | 0 | } |
57 | |
finally |
58 | |
{ |
59 | 0 | transactions.set(null); |
60 | |
|
61 | 0 | synchronized (this) |
62 | |
{ |
63 | 0 | if (txCounter > 0) |
64 | |
{ |
65 | 0 | txCounter--; |
66 | |
} |
67 | 0 | } |
68 | 0 | } |
69 | 0 | } |
70 | |
|
71 | |
public void bindTransaction(UMOTransaction transaction) throws TransactionException |
72 | |
{ |
73 | 0 | UMOTransaction oldTx = (UMOTransaction) transactions.get(); |
74 | 0 | if (oldTx != null) |
75 | |
{ |
76 | 0 | throw new IllegalTransactionStateException(CoreMessages.transactionAlreadyBound()); |
77 | |
} |
78 | |
|
79 | 0 | transactions.set(transaction); |
80 | |
|
81 | 0 | synchronized (this) |
82 | |
{ |
83 | 0 | txCounter++; |
84 | |
|
85 | 0 | if (logger.isDebugEnabled()) |
86 | |
{ |
87 | 0 | logger.debug("Binding new transaction (" + txCounter + ")"); |
88 | |
} |
89 | 0 | } |
90 | 0 | } |
91 | |
|
92 | |
} |