Coverage Report - org.mule.transaction.TransactionCoordination
 
Classes in this File Line Coverage Branch Coverage Complexity
TransactionCoordination
0%
0/46
0%
0/22
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.transaction;
 8  
 
 9  
 import org.mule.api.transaction.Transaction;
 10  
 import org.mule.api.transaction.TransactionException;
 11  
 import org.mule.config.i18n.CoreMessages;
 12  
 
 13  
 import org.apache.commons.logging.Log;
 14  
 import org.apache.commons.logging.LogFactory;
 15  
 
 16  
 public final class TransactionCoordination
 17  
 {
 18  0
     protected static final Log logger = LogFactory.getLog(TransactionCoordination.class);
 19  
 
 20  0
     private static final TransactionCoordination instance = new TransactionCoordination();
 21  
 
 22  
     /**
 23  
      * This field could be static because it is a {@link ThreadLocal} and this class
 24  
      * is a singleton but, as it is used as an instance field by methods
 25  
      * {@link #getTransaction()}, {@link #unbindTransaction(Transaction)} and
 26  
      * {@link #bindTransaction(Transaction)}, it may be more consistent to have it as
 27  
      * an instance variable.
 28  
      */
 29  0
     private final ThreadLocal<Transaction> transactions = new ThreadLocal<Transaction>();
 30  
 
 31  
     /** Lock variable that is used to access {@link #txCounter}. */
 32  0
     private final Object txCounterLock = new Object();
 33  
 
 34  
     /** The access to this field is guarded by {@link #txCounterLock}. */
 35  0
     private int txCounter = 0;
 36  
 
 37  
     /** Do not instanciate. */
 38  
     private TransactionCoordination()
 39  
     {
 40  0
         super();
 41  0
     }
 42  
 
 43  
     public static TransactionCoordination getInstance()
 44  
     {
 45  0
         return instance;
 46  
     }
 47  
 
 48  
     public Transaction getTransaction()
 49  
     {
 50  0
         return transactions.get();
 51  
     }
 52  
 
 53  
     public void unbindTransaction(final Transaction transaction) throws TransactionException
 54  
     {
 55  0
         Transaction oldTx = transactions.get();
 56  
 
 57  0
         if (oldTx instanceof TransactionCollection)
 58  
         {
 59  
             // if there are more in-flight aggregated transactions, do nothing yet
 60  0
             if (!((TransactionCollection) oldTx).getTxCollection().isEmpty())
 61  
             {
 62  0
                 return;
 63  
             }
 64  
         }
 65  
 
 66  
         try
 67  
         {
 68  0
             if (oldTx != null && !oldTx.equals(transaction))
 69  
             {
 70  0
                 throw new IllegalTransactionStateException(CoreMessages.transactionCannotUnbind());
 71  
             }
 72  
         }
 73  
         finally
 74  
         {
 75  0
             transactions.set(null);
 76  0
             logTransactionUnbound(transaction);
 77  0
         }
 78  0
     }
 79  
 
 80  
     private void logTransactionUnbound(final Transaction transaction)
 81  
     {
 82  
         // We store the txCounter in a local variable to minimize locking
 83  0
         int txCounter = 0;
 84  0
         synchronized (txCounterLock)
 85  
         {
 86  0
             if (this.txCounter > 0)
 87  
             {
 88  0
                 txCounter = --this.txCounter;
 89  
             }
 90  0
         }
 91  
 
 92  0
         if (logger.isDebugEnabled())
 93  
         {
 94  0
             logger.debug("Unbinding transaction (" + txCounter + ") " + transaction);
 95  
         }
 96  0
     }
 97  
 
 98  
     public void bindTransaction(final Transaction transaction) throws TransactionException
 99  
     {
 100  0
         Transaction oldTx = transactions.get();
 101  
         // special handling for transaction collection
 102  0
         if (oldTx != null && !(oldTx instanceof TransactionCollection))
 103  
         {
 104  0
             throw new IllegalTransactionStateException(CoreMessages.transactionAlreadyBound());
 105  
         }
 106  
 
 107  0
         if (oldTx instanceof TransactionCollection)
 108  
         {
 109  0
             TransactionCollection txCollection = (TransactionCollection) oldTx;
 110  0
             if (txCollection.getTxCollection().contains(transaction)) {
 111  
                 // TODO improve the error message with more TX details
 112  0
                 throw new IllegalTransactionStateException(CoreMessages.transactionAlreadyBound());
 113  
             }
 114  
             else
 115  
             {
 116  
                 // will be aggregated next
 117  0
                 return;
 118  
             }
 119  
         }
 120  
 
 121  0
         transactions.set(transaction);
 122  0
         logTransactionBound(transaction);
 123  0
     }
 124  
 
 125  
     private void logTransactionBound(final Transaction transaction)
 126  
     {
 127  
         // We store the txCounter in a local variable to minimize locking
 128  
         int txCounter;
 129  0
         synchronized (txCounterLock)
 130  
         {
 131  0
             txCounter = ++this.txCounter;
 132  0
         }
 133  
 
 134  0
         if (logger.isDebugEnabled())
 135  
         {
 136  0
             logger.debug("Binding new transaction (" + txCounter + ") " + transaction);
 137  
         }
 138  0
     }
 139  
 
 140  
     public void clear()
 141  
     {
 142  0
         transactions.remove();
 143  0
     }
 144  
 }