Coverage Report - org.mule.transaction.TransactionCoordination
 
Classes in this File Line Coverage Branch Coverage Complexity
TransactionCoordination
93%
26/28
80%
8/10
2.2
 
 1  
 /*
 2  
  * $Id: TransactionCoordination.java 10489 2008-01-23 17:53:38Z dfeist $
 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 CPAL v1.0
 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.mule.api.transaction.Transaction;
 14  
 import org.mule.api.transaction.TransactionException;
 15  
 import org.mule.config.i18n.CoreMessages;
 16  
 
 17  
 import org.apache.commons.logging.Log;
 18  
 import org.apache.commons.logging.LogFactory;
 19  
 
 20  
 public final class TransactionCoordination
 21  
 {
 22  2
     protected static final Log logger = LogFactory.getLog(TransactionCoordination.class);
 23  
 
 24  2
     private static final TransactionCoordination instance = new TransactionCoordination();
 25  
 
 26  2
     private static final ThreadLocal transactions = new ThreadLocal();
 27  
 
 28  
     // @GuardedBy("this")
 29  2
     private int txCounter = 0;
 30  
 
 31  
     /** Do not instanciate. */
 32  
     private TransactionCoordination()
 33  
     {
 34  2
         super();
 35  2
     }
 36  
 
 37  
     public static TransactionCoordination getInstance()
 38  
     {
 39  144
         return instance;
 40  
     }
 41  
 
 42  
     public Transaction getTransaction()
 43  
     {
 44  150
         return (Transaction) transactions.get();
 45  
     }
 46  
 
 47  
     public void unbindTransaction(Transaction transaction) throws TransactionException
 48  
     {
 49  
         try
 50  
         {
 51  20
             Transaction oldTx = (Transaction) transactions.get();
 52  20
             if (oldTx != null && !oldTx.equals(transaction))
 53  
             {
 54  0
                 throw new IllegalTransactionStateException(CoreMessages.transactionCannotUnbind());
 55  
             }
 56  
         }
 57  
         finally
 58  
         {
 59  20
             transactions.set(null);
 60  
 
 61  20
             synchronized (this)
 62  
             {
 63  20
                 if (txCounter > 0)
 64  
                 {
 65  8
                     txCounter--;
 66  
                 }
 67  20
             }
 68  20
         }
 69  20
     }
 70  
 
 71  
     public void bindTransaction(Transaction transaction) throws TransactionException
 72  
     {
 73  10
         Transaction oldTx = (Transaction) transactions.get();
 74  10
         if (oldTx != null)
 75  
         {
 76  2
             throw new IllegalTransactionStateException(CoreMessages.transactionAlreadyBound());
 77  
         }
 78  
 
 79  8
         transactions.set(transaction);
 80  
 
 81  8
         synchronized (this)
 82  
         {
 83  8
             txCounter++;
 84  
 
 85  8
             if (logger.isDebugEnabled())
 86  
             {
 87  0
                 logger.debug("Binding new transaction (" + txCounter + ") " + transaction);
 88  
             }
 89  8
         }
 90  8
     }
 91  
 
 92  
 }