// If we have only one resource, we don't ask to prepare, and we go with one-phase commit
status = Status.STATUS_COMMITTING;
while (branchKeys.hasNext())
{
Xid key = branchKeys.next();
XAResource resourceManager = branches.get(key);
try
{
if (!failed)
{
resourceManager.commit(key, true);
}
else
{
resourceManager.rollback(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;
NucleusLogger.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()))
{
Xid key = branchKeys.next();
XAResource resourceManager = branches.get(key);
try
{
// Preparing the resource manager using its branch xid
resourceManager.prepare(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;
NucleusLogger.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())
{
Xid key = branchKeys.next();
XAResource resourceManager = branches.get(key);
try
{
resourceManager.rollback(key);
}
catch (Throwable e)
{
NucleusLogger.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())
{
Xid key = branchKeys.next();
XAResource resourceManager = branches.get(key);
try
{
resourceManager.commit(key, false);
}
catch (Throwable e)
{
NucleusLogger.TRANSACTION.error(LOCALISER.msg("015038", "commit", resourceManager,
getXAErrorCode(e), toString()));