Java Code Examples for javax.transaction.TransactionSynchronizationRegistry#getResource()

The following examples show how to use javax.transaction.TransactionSynchronizationRegistry#getResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ManagedDataSource.java    From tomee with Apache License 2.0 5 votes vote down vote up
protected static Connection getTxConnection(final CommonDataSource delegate, final String u, final String p, final TransactionManager transactionManager, final TransactionSynchronizationRegistry registry) {
    try {
        final Transaction transaction = transactionManager.getTransaction();
        if (transaction != null && ManagedConnection.isUnderTransaction(transaction.getStatus())) {
            final Object resource = registry.getResource(new Key(delegate, u, p));
            if (Connection.class.isInstance(resource)) {
                return Connection.class.cast(resource);
            }
        }
    } catch (SystemException e) {
        // we wouldn't expect this to happen, but lets log it and fall through to the previous behaviour
        LOGGER.warning("Attempting to get the current transaction failed with an error: " + e.getMessage(), e);
    }
    return null;
}
 
Example 2
Source File: AbstractPool.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Gets connection listener instance associated with transaction.
 * This method is package protected beacause it is intended only for test case use.
 * Please don't use it in your production code.
 * @param trackByTransaction transaction instance
 * @param mcp the managed connection pool associated with the desired connection listener
 * @return connection listener instance
 * @throws ResourceException Thrown if an error occurs
 */
ConnectionListener getTransactionOldConnection(Transaction trackByTransaction, ManagedConnectionPool mcp)
   throws ResourceException
{
   TransactionSynchronizationRegistry tsr = getTransactionSynchronizationRegistry();
   Lock lock = getTSRLock();

   if (lock == null)
      throw new ResourceException(bundle.unableObtainLock());

   try
   {
      lock.lockInterruptibly();
   }
   catch (InterruptedException ie)
   {
      Thread.interrupted();
      
      throw new ResourceException(bundle.unableObtainLock(), ie);
   }
   try
   {
      // Already got one
      ConnectionListener cl = (ConnectionListener)tsr.getResource(mcp);
      if (cl != null)
      {
         log.tracef("Previous connection tracked by transaction=%s tx=%s", cl, trackByTransaction);
         return cl;
      }

      return null;
   }
   catch (Throwable t)
   {
      throw new ResourceException(bundle.unableGetConnectionListener(), t);
   }
   finally
   {
      lock.unlock();
   }
}
 
Example 3
Source File: AbstractPool.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean hasConnection(Subject subject, ConnectionRequestInfo cri)
{
   TransactionSynchronizationRegistry tsr = getTransactionSynchronizationRegistry();
   Lock lock = getTSRLock();

   if (lock == null)
      return false;

   try
   {
      lock.lockInterruptibly();
   }
   catch (InterruptedException ie)
   {
      Thread.interrupted();
      return false;
   }
   try
   {
      boolean separateNoTx = false;

      if (noTxSeparatePools)
      {
         separateNoTx = cm.isTransactional();
      }

      // Get specific managed connection pool key
      Object key = getKey(subject, cri, separateNoTx);

      // Get managed connection pool
      ManagedConnectionPool mcp = getManagedConnectionPool(key, subject, cri);

      // Already got one
      ConnectionListener cl = (ConnectionListener)tsr.getResource(mcp);
      if (cl != null)
      {
         return true;
      }
   }
   catch (Throwable t)
   {
      log.debugf(t, "hasConnection error: %s", t.getMessage());
   }
   finally
   {
      lock.unlock();
   }

   return false;
}
 
Example 4
Source File: TxConnectionListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void dissociate() throws ResourceException
{
   log.tracef("dissociate: %s", this);

   try
   {
      TransactionManager tm = getConnectionManager().getTransactionIntegration().getTransactionManager();
      int status = tm.getStatus();

      log.tracef("dissociate: status=%s", TxUtils.getStatusAsString(status));

      if (status != Status.STATUS_NO_TRANSACTION)
      {
         if (isEnlisted())
         {
            if (doDelistResource)
            {
               Transaction tx = tm.getTransaction();
               boolean delistResult = tx.delistResource(getXAResource(), XAResource.TMSUCCESS);

               log.tracef("dissociate: delistResult=%s", delistResult);
            }
         }
         else
         {
            log.tracef("dissociate: not enlisted (%s)", this);
         }

         if (isTrackByTx())
         {
            ManagedConnectionPool mcp = getManagedConnectionPool();
            TransactionSynchronizationRegistry tsr =
               getConnectionManager().getTransactionIntegration().getTransactionSynchronizationRegistry();

            Lock lock = (Lock)tsr.getResource(LockKey.INSTANCE);
            if (lock != null)
            {
               try
               {
                  lock.lockInterruptibly();
               }
               catch (InterruptedException ie)
               {
                  Thread.interrupted();
                  
                  throw new ResourceException(bundle.unableObtainLock(), ie);
               }

               try
               {
                  tsr.putResource(mcp, null);
               }
               finally
               {
                  lock.unlock();
               }
            }
         }
      }

      localTransaction.set(false);
      setTrackByTx(false);
   
      if (transactionSynchronization != null)
      {
         transactionSynchronization.cancel();
         transactionSynchronization = null;
      }

      setEnlisted(false);
   }
   catch (Throwable t)
   {
      throw new ResourceException(bundle.errorInDissociate(), t);
   }
}