public void rollbackTransaction(AbstractTransactionContext context) throws ResourceManagerException { assureReady(); synchronized (context) { if (logger.isDebugEnabled()) { logger.debug("Rolling back transaction " + context); } try { context.status = Status.STATUS_ROLLING_BACK; doRollback(context); context.status = Status.STATUS_ROLLEDBACK; } catch (Error e) { setDirty(context, e); throw e; } catch (RuntimeException e) { setDirty(context, e); throw e; } catch (ResourceManagerSystemException e) { setDirty(context, e); throw e; } finally { globalTransactions.remove(context); context.finalCleanUp(); // tell shutdown thread this tx is finished context.notifyFinish(); } if (logger.isDebugEnabled()) { logger.debug("Rolled back transaction " + context); } } }
protected boolean waitForAllTxToStop(long timeoutMSecs) { long startTime = System.currentTimeMillis(); // be sure not to lock globalTransactions for too long, as we need to // give // txs the chance to complete (otherwise deadlocks are very likely to // occur) // instead iterate over a copy as we can be sure no new txs will be // registered // after operation level has been set to stopping Collection transactionsToStop; synchronized (globalTransactions) { transactionsToStop = new ArrayList(globalTransactions); } for (Iterator it = transactionsToStop.iterator(); it.hasNext(); ) { long remainingTimeout = startTime - System.currentTimeMillis() + timeoutMSecs; if (remainingTimeout <= 0) { return false; } AbstractTransactionContext context = (AbstractTransactionContext) it.next(); synchronized (context) { if (!context.finished) { logger.info( "Waiting for tx " + context + " to finish for " + remainingTimeout + " milli seconds"); } while (!context.finished && remainingTimeout > 0) { try { context.wait(remainingTimeout); } catch (InterruptedException e) { return false; } remainingTimeout = startTime - System.currentTimeMillis() + timeoutMSecs; } if (context.finished) { logger.info("Tx " + context + " finished"); } else { logger.warn("Tx " + context + " failed to finish in given time"); } } } return (globalTransactions.size() == 0); }
public int prepareTransaction(AbstractTransactionContext context) throws ResourceManagerException { assureReady(); synchronized (context) { if (logger.isDebugEnabled()) { logger.debug("Preparing transaction " + context); } context.status = Status.STATUS_PREPARING; int status = doPrepare(context); context.status = Status.STATUS_PREPARED; if (logger.isDebugEnabled()) { logger.debug("Prepared transaction " + context); } return status; } }
public void commitTransaction(AbstractTransactionContext context) throws ResourceManagerException { assureReady(); if (context.status == Status.STATUS_MARKED_ROLLBACK) { throw new ResourceManagerException(new Message(Messages.TX_MARKED_FOR_ROLLBACK)); } synchronized (context) { if (logger.isDebugEnabled()) { logger.debug("Committing transaction " + context); } try { context.status = Status.STATUS_COMMITTING; doCommit(context); context.status = Status.STATUS_COMMITTED; } catch (Error e) { setDirty(context, e); throw e; } catch (RuntimeException e) { setDirty(context, e); throw e; } catch (ResourceManagerSystemException e) { setDirty(context, e); throw e; } catch (ResourceManagerException e) { logger.warn("Could not commit tx " + context + ", rolling back instead", e); doRollback(context); } finally { globalTransactions.remove(context); context.finalCleanUp(); // tell shutdown thread this tx is finished context.notifyFinish(); } if (logger.isDebugEnabled()) { logger.debug("Committed transaction " + context); } } }
public void beginTransaction(AbstractTransactionContext context) throws ResourceManagerException { assureStarted(); // can only start a new transaction when not already // stopping synchronized (context) { if (logger.isDebugEnabled()) { logger.debug("Beginning transaction " + context); } doBegin(context); context.status = Status.STATUS_ACTIVE; if (logger.isDebugEnabled()) { logger.debug("Begun transaction " + context); } } globalTransactions.add(context); }
public void setTransactionRollbackOnly(AbstractTransactionContext context) throws ResourceManagerException { context.status = Status.STATUS_MARKED_ROLLBACK; }