KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transaction > TransactionCoordination


1 /*
2  * $Id: TransactionCoordination.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.transaction;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.config.i18n.Message;
16 import org.mule.config.i18n.Messages;
17 import org.mule.umo.TransactionException;
18 import org.mule.umo.UMOTransaction;
19
20 public class TransactionCoordination
21 {
22     protected static final Log logger = LogFactory.getLog(TransactionCoordination.class);
23
24     private static volatile TransactionCoordination instance;
25
26     private final ThreadLocal JavaDoc transactions = new ThreadLocal JavaDoc();
27     private int txCounter = 0;
28
29     private TransactionCoordination()
30     {
31         super();
32     }
33
34     public static TransactionCoordination getInstance()
35     {
36         if (instance == null)
37         {
38             setInstance(new TransactionCoordination());
39         }
40
41         return instance;
42     }
43
44     public static synchronized void setInstance(TransactionCoordination txSync)
45     {
46         if (instance == null)
47         {
48             instance = txSync;
49         }
50
51         synchronized (instance)
52         {
53             if (instance.txCounter != 0)
54             {
55                 throw new IllegalStateException JavaDoc("there are currently " + instance.txCounter
56                                 + "transactions associated with this manager, cannot replace the manager");
57             }
58         }
59     }
60
61     public UMOTransaction getTransaction()
62     {
63         return (UMOTransaction)transactions.get();
64     }
65
66     public void unbindTransaction(UMOTransaction transaction) throws TransactionException
67     {
68         try
69         {
70             UMOTransaction oldTx = (UMOTransaction)transactions.get();
71             if (oldTx != null && !oldTx.equals(transaction))
72             {
73                 throw new IllegalTransactionStateException(new Message(Messages.TX_CANT_UNBIND));
74             }
75         }
76         finally
77         {
78             transactions.set(null);
79
80             synchronized (this)
81             {
82                 if (txCounter > 0)
83                 {
84                     txCounter--;
85                 }
86             }
87         }
88     }
89
90     public void bindTransaction(UMOTransaction transaction) throws TransactionException
91     {
92         UMOTransaction oldTx = (UMOTransaction)transactions.get();
93         if (oldTx != null)
94         {
95             throw new IllegalTransactionStateException(new Message(Messages.TX_CANT_BIND_ALREADY_BOUND));
96         }
97
98         transactions.set(transaction);
99
100         synchronized (this)
101         {
102             txCounter++;
103
104             if (logger.isDebugEnabled())
105             {
106                 logger.debug("Binding new transaction (" + txCounter + ")");
107             }
108         }
109     }
110
111 }
112
Popular Tags