// Our synchronization
TransactionSynchronization ourSynchronization = null;
// Serializes enlistment when two different threads are enlisting
// different connections in the same transaction concurrently
TransactionSynchronizer synchronizer = null;
try
{
TransactionSynchronizer.lock(threadTx,
getConnectionManager().getTransactionIntegration()
.getTransactionSynchronizationRegistry());
}
catch (Exception e)
{
setTrackByTx(false);
TxConnectionManagerImpl.rethrowAsSystemException("Exception during lock", threadTx, e);
}
try
{
// Interleaving should have an unenlisted transaction
// TODO We should be able to do some sharing shouldn't we?
if (!isTrackByTx() && transactionSynchronization != null)
{
String error = "Can't enlist - already a tx!";
if (trace)
{
log.trace(error + " " + this);
}
throw new IllegalStateException(error);
}
// Check for different transaction
if (transactionSynchronization != null && !transactionSynchronization.currentTx.equals(threadTx))
{
String error = "Trying to change transaction " + threadTx + " in enlist!";
if (trace)
{
log.trace(error + " " + this);
}
throw new IllegalStateException(error);
}
// Get the synchronizer
try
{
if (this.trace)
{
log.trace("Get synchronizer " + this + " threadTx=" + threadTx);
}
synchronizer =
TransactionSynchronizer.getRegisteredSynchronizer(threadTx,
getConnectionManager().getTransactionIntegration().getTransactionSynchronizationRegistry());
}
catch (Throwable t)
{
setTrackByTx(false);
TxConnectionManagerImpl.rethrowAsSystemException("Cannot register synchronization", threadTx, t);
}
// First time through, create a transaction synchronization
if (transactionSynchronization == null)
{
TransactionSynchronization synchronization = new TransactionSynchronization(threadTx, isTrackByTx());
synchronizer.addUnenlisted(synchronization);
transactionSynchronization = synchronization;
}
ourSynchronization = transactionSynchronization;
}
finally
{
TransactionSynchronizer.unlock(threadTx);
}
// Perform the enlistment(s)
List<Synchronization> unenlisted = synchronizer.getUnenlisted();
if (unenlisted != null)
{
try
{
int size = unenlisted.size();
for (int i = 0; i < size; ++i)
{
TransactionSynchronization sync = (TransactionSynchronization) unenlisted.get(i);
if (sync.enlist())
{
synchronizer.addEnlisted(sync);
}
}
}
finally
{
synchronizer.enlisted();
}
}
// What was the result of our enlistment?
if (this.trace)