Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.resource.connectionmanager;
 
 
 
Organizes transaction synchronization done by JCA.

This class exists to make sure all Tx synchronizations are invoked before the cached connection manager closes any closed connections.

Author(s):
Adrian Brock
Version:
$Revision: 71554 $
 
 public class TransactionSynchronizer implements Synchronization
 {
   
The logger
 
    private static final Logger log = Logger.getLogger(TransactionSynchronizer.class);

   
The transaction synchronizations
 
    protected static TransactionLocal txSynchs;
   
   
The transaction
 
    protected Transaction tx;
   
   
The enlisting thread
 
    protected Thread enlistingThread;
   
   
Unenlisted
 
    protected ArrayList unenlisted;
   
   
Enlisted
 
    protected ArrayList enlisted;
   
   
The cached connection manager synchronization
 
    protected Synchronization ccmSynch;

   
Initialization
 
    public static void setTransactionManager(TransactionManager tm)
    {
        = new TransactionLocal(tm);
    }
   
   
Create a new transaction synchronizer

Parameters:
tx the transaction to synchronize with
 
    {
       this. = tx;
    }
   
   
Add a new Tx synchronization that has not been enlisted

Parameters:
synch the synchronization
 
    synchronized void addUnenlisted(Synchronization synch)
    {
       if ( == null)
           = new ArrayList();
       .add(synch);
    }
   
   
Get the unenlisted synchronizations and say we are enlisting if some are returned.

Returns:
the unenlisted synchronizations
   synchronized ArrayList getUnenlisted()
   {
      Thread currentThread = Thread.currentThread();
      while ( != null &&  != currentThread)
      {
         boolean interrupted = false;
         try
         {
            wait();
         }
         catch (InterruptedException e)
         {
            interrupted = true;
         }
         if (interrupted)
            currentThread.interrupt();
      }
      ArrayList result = ;
       = null;
      if (result != null)
          = currentThread;
      return result;
   }
   
   
The synchronization is now enlisted

Parameters:
synch the synchronization
   synchronized void addEnlisted(Synchronization synch)
   {
      if ( == null)
          = new ArrayList();
      .add(synch);
   }
   
   
Remove an enlisted synchronization

Parameters:
synch the synchronization
Returns:
true when the synchronization was enlisted
   synchronized boolean removeEnlisted(Synchronization synch)
   {
      return .remove(synch);
   }
   
   
This thread has finished enlisting
   synchronized void enlisted()
   {
      Thread currentThread = Thread.currentThread();
      if ( == null ||  != currentThread)
      {
         .warn("Thread " + currentThread + " not the enlisting thread " + new Exception("STACKTRACE"));
         return;
      }
       = null;
      notifyAll();
   }
   
   
Get a registered transaction synchronizer.

Parameters:
tx the transaction
Returns:
the registered transaction synchronizer for this transaction
Throws:
RolledbackException if the transaction is already rolled back
javax.transaction.SystemException for an error in the tranaction manager
   {
      if (result == null)
      {
         result = new TransactionSynchronizer(tx);
         tx.registerSynchronization(result);
         .set(txresult);
      }
      return result;
   }
   
   
Check whether we have a CCM synchronization

Parameters:
tx the transaction
   {
      if (ts != null)
         return ts.ccmSynch;
      else
         return null;
   }
   
   
Register a new CCM synchronization

Parameters:
tx the transaction
synch the synchronization
Throws:
RolledbackException if the transaction is already rolled back
javax.transaction.SystemException for an error in the tranaction manager
   {
      ts.ccmSynch = synch;
   }
   
   
Lock for the given transaction

Parameters:
tx the transaction
   static void lock(Transaction tx)
   {
      try
      {
         .lock(tx);
      }
      catch (InterruptedException e)
      {
         throw new NestedRuntimeException("Unable to get synchronization"e);
      }
   }
   
   
Unlock for the given transaction

Parameters:
tx the transaction
   static void unlock(Transaction tx)
   {
      .unlock(tx);
   }
   public void beforeCompletion()
   {
      if ( != null)
      {
         int i = 0;
         while (i < .size())
         {
            Synchronization synch = (Synchronization.get(i);
            invokeBefore(synch);
            ++i;
         }
      }
      
      if ( != null)
         invokeBefore();
   }
   public void afterCompletion(int status)
   {
      if ( != null)
      {
         int i = 0;
         while (i < .size())
         {
            Synchronization synch = (Synchronization.get(i);
            invokeAfter(synchstatus);
            ++i;
         }
      }
      
      if ( != null)
         invokeAfter(status);
   }

   
Invoke a beforeCompletion

Parameters:
synch the synchronization
   protected void invokeBefore(Synchronization synch)
   {
      try
      {
         synch.beforeCompletion();
      }
      catch (Throwable t)
      {
         .warn("Transaction " +  + " error in before completion " + syncht);
      }
   }

   
Invoke an afterCompletion

Parameters:
synch the synchronization
status the status of the transaction
   protected void invokeAfter(Synchronization synchint status)
   {
      try
      {
         synch.afterCompletion(status);
      }
      catch (Throwable t)
      {
         .warn("Transaction " +  + " error in after completion " + syncht);
      }
   }
New to GrepCode? Check out our FAQ X