// Synchronization.beforeCompletion
Iterator syncIterator = synchronization.iterator();
while (syncIterator.hasNext())
{
Synchronization sync = (Synchronization) syncIterator.next();
sync.beforeCompletion();
}
List failures = null;
boolean failed = false;
Iterator branchKeys = branches.keySet().iterator();
if (enlistedResources.size() == 1)
{
// If we have only one resource, we dont ask to prepare, and we go with one phase commit
status = Status.STATUS_COMMITTING;
while (branchKeys.hasNext())
{
Object key = branchKeys.next();
XAResource resourceManager = (XAResource) branches.get(key);
try
{
if (!failed)
{
resourceManager.commit(xid, true);
}
else
{
resourceManager.rollback(xid);
}
}
catch (Throwable e)
{
if( failures == null )
{
//lazy instantiate this, because we only need on failures
failures = new ArrayList();
}
failures.add(e);
failed = true;
status = Status.STATUS_MARKED_ROLLBACK;
JPOXLogger.TRANSACTION.error(LOCALISER.msg("015038", "commit", resourceManager, getXAErrorCode(e), toString()));
}
}
if (!failed)
{
status = Status.STATUS_COMMITTED;
}
else
{
status = Status.STATUS_ROLLEDBACK;
}
}
else if (enlistedResources.size() > 0)
{
// Prepare each enlisted resource
status = Status.STATUS_PREPARING;
while ((!failed) && (branchKeys.hasNext()))
{
Object key = branchKeys.next();
XAResource resourceManager = (XAResource) branches.get(key);
try
{
// Preparing the resource manager using its branch xid
resourceManager.prepare((Xid) key);
}
catch (Throwable e)
{
if( failures == null )
{
//lazy instantiate this, because we only need on failures
failures = new ArrayList();
}
failures.add(e);
failed = true;
status = Status.STATUS_MARKED_ROLLBACK;
JPOXLogger.TRANSACTION.error(LOCALISER.msg("015038", "prepare", resourceManager, getXAErrorCode(e), toString()));
}
}
if (!failed)
{
status = Status.STATUS_PREPARED;
}
// Starts 2nd commit phase
// If fail, rollback
if (failed)
{
status = Status.STATUS_ROLLING_BACK;
failed = false;
// Rolling back all the prepared (and unprepared) branches
branchKeys = branches.keySet().iterator();
while (branchKeys.hasNext())
{
Object key = branchKeys.next();
XAResource resourceManager = (XAResource) branches.get(key);
try
{
resourceManager.rollback((Xid) key);
}
catch (Throwable e)
{
JPOXLogger.TRANSACTION.error(LOCALISER.msg("015038", "rollback", resourceManager, getXAErrorCode(e), toString()));
if( failures == null )
{
//lazy instantiate this, because we only need on failures
failures = new ArrayList();
}
failures.add(e);
failed = true;
}
}
status = Status.STATUS_ROLLEDBACK;
}
else
{
status = Status.STATUS_COMMITTING;
// Commit each enlisted resource
branchKeys = branches.keySet().iterator();
while (branchKeys.hasNext())
{
Object key = branchKeys.next();
XAResource resourceManager = (XAResource) branches.get(key);
try
{
resourceManager.commit((Xid) key, false);
}
catch (Throwable e)
{
JPOXLogger.TRANSACTION.error(LOCALISER.msg("015038", "commit", resourceManager, getXAErrorCode(e), toString()));
if( failures == null )
{
//lazy instantiate this, because we only need on failures
failures = new ArrayList();
}
failures.add(e);
failed = true;
}
}
status = Status.STATUS_COMMITTED;
}
}
// Synchronization.afterCompletion
syncIterator = synchronization.iterator();
while (syncIterator.hasNext())
{
Synchronization sync = (Synchronization) syncIterator.next();
sync.afterCompletion(status);
}
if (status == Status.STATUS_ROLLEDBACK)
{
if( failed )