/** * Mark this index for roll back only. This action can not be reversed. It will reject all other work and only allow * roll back. */ public void setRollbackOnly() { switch (getStatus().getStatus()) { case Status.STATUS_COMMITTING: throw new IndexerException("Unable to mark for rollback: Transaction is committing"); case Status.STATUS_COMMITTED: throw new IndexerException("Unable to mark for rollback: Transaction is committed"); default: try { doSetRollbackOnly(); setStatus(TransactionStatus.MARKED_ROLLBACK); } catch (IOException e) { throw new LuceneIndexException("Set rollback only failed ", e); } break; } }
@Override protected void tearDown() throws Exception { deletePerson(USER_BOB); deletePerson(USER_TOM); deletePerson(USER_LISA); if (txn != null) { if (txn.getStatus() == Status.STATUS_MARKED_ROLLBACK) { txn.rollback(); } else { txn.commit(); } txn = null; } }
@After @Override public void tearDown() throws Exception { scheduler.getScheduler().unscheduleJob(scheduler.getTriggerName(), scheduler.getJobGroup()); if (Status.STATUS_ROLLEDBACK != transaction.getStatus()) { transaction.rollback(); } freshNodes.clear(); yesterdayNodes.clear(); AuthenticationUtil.clearCurrentSecurityContext(); }
@Test public void test_RollbackExceptionHandling_rollbackafterthrown() throws Exception { TransactionManager tm = mockTm(); when(tm.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_MARKED_ROLLBACK); doThrow(new RollbackException().initCause(new OptimisticLockException())).when(tm).commit(); XAJpaTemplate tx = new XAJpaTemplate(emSupplier, tm, coordinator); try { tx.tx(TransactionType.Required, new EmConsumer() { public void accept(EntityManager em) { em.persist(new Object()); } }); } catch (RuntimeException e) { // this is ok } verify(tm, times(5)).getStatus(); verify(tm, times(1)).commit(); verify(tm, times(1)).rollback(); }
public void testSimpleTxnWithRollback() throws Exception { testNoTxnStatus(); try { txn.begin(); throw new Exception("Blah"); } catch (Throwable e) { txn.rollback(); } assertEquals("Transaction status is not correct", Status.STATUS_ROLLEDBACK, txn.getStatus()); assertEquals("Transaction manager not called correctly", txn.getStatus(), transactionManager.getStatus()); checkNoStatusOnThread(); }
/** * JTA {@code afterCompletion} callback: invoked after commit/rollback. * <p>Needs to invoke the Spring synchronization's {@code beforeCompletion} * at this late stage in case of a rollback, since there is no corresponding * callback with JTA. * @see org.springframework.transaction.support.TransactionSynchronization#beforeCompletion * @see org.springframework.transaction.support.TransactionSynchronization#afterCompletion */ @Override public void afterCompletion(int status) { if (!this.beforeCompletionCalled) { // beforeCompletion not called before (probably because of JTA rollback). // Perform the cleanup here. this.springSynchronization.beforeCompletion(); } // Call afterCompletion with the appropriate status indication. switch (status) { case Status.STATUS_COMMITTED: this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_COMMITTED); break; case Status.STATUS_ROLLEDBACK: this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK); break; default: this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN); } }
@Override public void afterCompletion(int status) { switch (status) { case Status.STATUS_COMMITTED: try { TransactionSynchronizationUtils.invokeAfterCommit(this.synchronizations); } finally { TransactionSynchronizationUtils.invokeAfterCompletion( this.synchronizations, TransactionSynchronization.STATUS_COMMITTED); } break; case Status.STATUS_ROLLEDBACK: TransactionSynchronizationUtils.invokeAfterCompletion( this.synchronizations, TransactionSynchronization.STATUS_ROLLED_BACK); break; default: TransactionSynchronizationUtils.invokeAfterCompletion( this.synchronizations, TransactionSynchronization.STATUS_UNKNOWN); } }
/** * If the given UserTransaction is not null, it is committed/rolledback, * and then returned to the UserTransactionHelper. */ private void resolveUserTransaction(UserTransaction userTransaction) { if (userTransaction != null) { try { if (userTransaction.getStatus() == Status.STATUS_MARKED_ROLLBACK) { userTransaction.rollback(); } else { userTransaction.commit(); } } catch (Throwable t) { getLog().error("Failed to resolve UserTransaction for plugin: " + getName(), t); } finally { UserTransactionHelper.returnUserTransaction(userTransaction); } } }
@Override public void commit() throws HibernateException { if ( localStatus != LocalStatus.ACTIVE ) { throw new TransactionException( "Transaction not successfully started" ); } LOG.debug( "committing" ); beforeTransactionCommit(); try { doCommit(); localStatus = LocalStatus.COMMITTED; afterTransactionCompletion( Status.STATUS_COMMITTED ); } catch (Exception e) { localStatus = LocalStatus.FAILED_COMMIT; afterTransactionCompletion( Status.STATUS_UNKNOWN ); throw new TransactionException( "commit failed", e ); } finally { invalidate(); afterAfterCompletion(); } }
@Override protected void doBegin() { LOG.debug( "begin" ); userTransaction = locateUserTransaction(); try { if ( userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION ) { userTransaction.begin(); isInitiator = true; LOG.debug( "Began a new JTA transaction" ); } } catch ( Exception e ) { throw new TransactionException( "JTA transaction begin failed", e ); } }
@Override public void afterCompletion(int status) { try { TXCommitMessage txcm = this.firstProxy.afterCompletion(status, proxy.getTxId().getUniqId()); if (status == Status.STATUS_COMMITTED) { if (txcm == null) { throw new TransactionInDoubtException( LocalizedStrings.ClientTXStateStub_COMMIT_FAILED_ON_SERVER.toLocalizedString()); } else { afterServerCommit(txcm); } } else if (status == Status.STATUS_ROLLEDBACK) { if (this.internalAfterSendRollback != null) { this.internalAfterSendRollback.run(); } this.firstProxy.getPool().releaseServerAffinity(); } } finally { if (status == Status.STATUS_COMMITTED) { // rollback does not grab locks this.lockReq.releaseLocal(); } this.firstProxy.getPool().releaseServerAffinity(); } }
/** * Commits or rolls back or does nothing with the current transaction and begins a new {@link UserTransaction} * * @param transactionAction - one of the {@link TransactionActionEnum} values which specifies action to be done for the current transaction * @throws Exception */ private void restartTransaction(TransactionActionEnum transactionAction) throws Exception { if ((null != transaction) && (Status.STATUS_ROLLEDBACK != transaction.getStatus()) && (Status.STATUS_COMMITTED != transaction.getStatus())) { if (TransactionActionEnum.ACTION_COMMIT == transactionAction) { transaction.commit(); } else if (TransactionActionEnum.ACTION_ROLLBACK == transactionAction) { transaction.rollback(); } } transaction = transactionService.getUserTransaction(); transaction.begin(); }
/** * Utility method to report errors about invalid state. * * @return - an error based on status */ private String buildErrorString() { StringBuilder buffer = new StringBuilder(128); buffer.append("The indexer is unable to accept more work: "); switch (getStatus().getStatus()) { case Status.STATUS_COMMITTED: buffer.append("The indexer has been committed"); break; case Status.STATUS_COMMITTING: buffer.append("The indexer is committing"); break; case Status.STATUS_MARKED_ROLLBACK: buffer.append("The indexer is marked for rollback"); break; case Status.STATUS_PREPARED: buffer.append("The indexer is prepared to commit"); break; case Status.STATUS_PREPARING: buffer.append("The indexer is preparing to commit"); break; case Status.STATUS_ROLLEDBACK: buffer.append("The indexer has been rolled back"); break; case Status.STATUS_ROLLING_BACK: buffer.append("The indexer is rolling back"); break; case Status.STATUS_UNKNOWN: buffer.append("The indexer is in an unknown state"); break; default: break; } return buffer.toString(); }
/** * Roll back the index changes (this just means they are never added) * * @throws LuceneIndexException */ public void rollback() throws LuceneIndexException { switch (getStatus().getStatus()) { case Status.STATUS_COMMITTED: throw new IndexerException("Unable to roll back: Transaction is committed "); case Status.STATUS_ROLLING_BACK: throw new IndexerException("Unable to roll back: Transaction is rolling back"); case Status.STATUS_ROLLEDBACK: throw new IndexerException("Unable to roll back: Transaction is already rolled back"); case Status.STATUS_COMMITTING: // Can roll back during commit default: try { setStatus(TransactionStatus.ROLLINGBACK); doRollBack(); setStatus(TransactionStatus.ROLLEDBACK); } catch (IOException e) { throw new LuceneIndexException("rollback failed ", e); } break; } }
protected void tearDown() throws Exception { if ((testTX.getStatus() == Status.STATUS_ACTIVE) || (testTX.getStatus() == Status.STATUS_MARKED_ROLLBACK)) { testTX.rollback(); } AuthenticationUtil.clearCurrentSecurityContext(); super.tearDown(); }
@Override protected void tearDown() throws Exception { if ((userTransaction.getStatus() == Status.STATUS_ACTIVE) || (userTransaction.getStatus() == Status.STATUS_MARKED_ROLLBACK)) { userTransaction.rollback(); } AuthenticationUtil.clearCurrentSecurityContext(); super.tearDown(); }
@Override protected void tearDown() throws Exception { // remove all the test users we created deleteTestUsers(); // cleanup transaction if necessary so we don't effect subsequent tests if ((userTransaction.getStatus() == Status.STATUS_ACTIVE) || (userTransaction.getStatus() == Status.STATUS_MARKED_ROLLBACK)) { userTransaction.rollback(); } AuthenticationUtil.clearCurrentSecurityContext(); super.tearDown(); }
@Override protected void tearDown() throws Exception { if ((tx.getStatus() == Status.STATUS_ACTIVE) || (tx.getStatus() == Status.STATUS_MARKED_ROLLBACK)) { tx.rollback(); } AuthenticationUtil.clearCurrentSecurityContext(); super.tearDown(); }
public void testPropagatingTxn() throws Exception { // start a transaction UserTransaction txnOuter = transactionService.getUserTransaction(); txnOuter.begin(); String txnIdOuter = AlfrescoTransactionSupport.getTransactionId(); // start a propagating txn UserTransaction txnInner = transactionService.getUserTransaction(); txnInner.begin(); String txnIdInner = AlfrescoTransactionSupport.getTransactionId(); // the txn IDs should be the same assertEquals("Txn ID not propagated", txnIdOuter, txnIdInner); // rollback the inner txnInner.rollback(); // check both transactions' status assertEquals("Inner txn not marked rolled back", Status.STATUS_ROLLEDBACK, txnInner.getStatus()); assertEquals("Outer txn not marked for rolled back", Status.STATUS_MARKED_ROLLBACK, txnOuter.getStatus()); try { txnOuter.commit(); fail("Outer txn not marked for rollback"); } catch (RollbackException e) { // expected txnOuter.rollback(); } }
@After public void teardown() throws Exception { if (txn.getStatus() == Status.STATUS_ACTIVE) { txn.rollback(); } }
public void tearDown() throws Exception { if (txn.getStatus() == Status.STATUS_ACTIVE) { txn.rollback(); } authenticationComponent.clearCurrentSecurityContext(); super.tearDown(); }
public void tearDown() throws Exception { try { if (txn.getStatus() != Status.STATUS_ROLLEDBACK && txn.getStatus() != Status.STATUS_COMMITTED) { txn.rollback(); } } catch (Throwable e) { e.printStackTrace(); } }
@After public void tearDown() throws Exception { try { if (txn.getStatus() != Status.STATUS_ROLLEDBACK && txn.getStatus() != Status.STATUS_COMMITTED) { txn.commit(); } } catch (Throwable e) { e.printStackTrace(); } }
@Override protected void tearDown() throws Exception { try { if (txn.getStatus() != Status.STATUS_ROLLEDBACK && txn.getStatus() != Status.STATUS_COMMITTED) { txn.rollback(); } } catch (Throwable e) { e.printStackTrace(); } }
@After @Override public void tearDown() throws Exception { final NodeRef rootTestFolder = expectedTagScopes.iterator().next(); for (String tagName : testTags) { taggingService.deleteTag(rootTestFolder.getStoreRef(), tagName); } testTags.clear(); testTags = null; if (Status.STATUS_ROLLEDBACK != transaction.getStatus()) { transaction.rollback(); } transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() { @Override public Void execute() throws Throwable { nodeService.deleteNode(rootTestFolder); return null; } }, false, true); AuthenticationUtil.clearCurrentSecurityContext(); expectedTagScopes.clear(); expectedTagScopes = null; }
public void testInTransactionRestore() throws Exception { RestoreNodeReport report = nodeArchiveService.restoreArchivedNode(a_); // expect a failure due to missing archive node assertEquals("Expected failure", RestoreStatus.FAILURE_INVALID_ARCHIVE_NODE, report.getStatus()); // check that our transaction was not affected assertEquals("Transaction should still be valid", Status.STATUS_ACTIVE, txn.getStatus()); }
@Override protected void tearDown() throws Exception { if (testTX.getStatus() == Status.STATUS_ACTIVE) { testTX.rollback(); } super.tearDown(); }
@Test public void test_RollbackExceptionHandling_rollbackiscalledonmarkedrollback() throws Exception { TransactionManager tm = mockTm(); when(tm.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_MARKED_ROLLBACK); XAJpaTemplate tx = new XAJpaTemplate(emSupplier, tm, coordinator); tx.tx(TransactionType.Required, new EmConsumer() { public void accept(EntityManager em) { em.persist(new Object()); } }); verify(tm, times(3)).getStatus(); verify(tm, never()).commit(); verify(tm, times(1)).rollback(); }
/** * {@inheritDoc} */ public boolean isActive() { try { return getTransactionSupport().getTransactionManager().getStatus() == Status.STATUS_ACTIVE; } catch (SystemException e) { throw new RuntimeException(e); } }
/** * Checks if the current transaction is active, rolled back or marked for * rollback. * * @return {@code true} if the current transaction is active, rolled back or * marked for rollback, {@code false} otherwise. * @throws SystemException thrown if the transaction manager encounters an * unexpected error condition */ private boolean isTransactionActive(TransactionManager transactionManager) throws SystemException { switch (transactionManager.getStatus()) { case Status.STATUS_ACTIVE: case Status.STATUS_MARKED_ROLLBACK: case Status.STATUS_ROLLEDBACK: return true; default: return false; } }
/** * Implementation required for {@link UserTransaction}. */ public void setTransactionTimeout(int timeout) throws SystemException { if (internalStatus != Status.STATUS_NO_TRANSACTION) { throw new RuntimeException("Can only set the timeout before begin"); } this.timeout = timeout; }
/** * This status is a combination of the internal status, as recorded during explicit operations, * and the status provided by the Spring support. * * @see Status */ public synchronized int getStatus() throws SystemException { TransactionInfo txnInfo = getTransactionInfo(); // if the txn info is null, then we are outside a transaction if (txnInfo == null) { return internalStatus; // this is checked in getTransactionInfo } // normally the internal status is correct, but we only need to double check // for the case where the transaction was marked for rollback, or rolledback // in a deeper transaction TransactionStatus txnStatus = txnInfo.getTransactionStatus(); if (internalStatus == Status.STATUS_ROLLEDBACK) { // explicitly rolled back at some point return internalStatus; } else if (txnStatus.isRollbackOnly()) { // marked for rollback at some point in the stack return Status.STATUS_MARKED_ROLLBACK; } else { // just rely on the internal status return internalStatus; } }
public synchronized void setRollbackOnly() throws IllegalStateException, SystemException { // just a check TransactionInfo txnInfo = getTransactionInfo(); int status = getStatus(); // check the status if (status == Status.STATUS_MARKED_ROLLBACK) { // this is acceptable } else if (status == Status.STATUS_NO_TRANSACTION) { throw new IllegalStateException("The transaction has not been started yet"); } else if (status == Status.STATUS_ROLLING_BACK || status == Status.STATUS_ROLLEDBACK) { throw new IllegalStateException("The transaction has already been rolled back"); } else if (status == Status.STATUS_COMMITTING || status == Status.STATUS_COMMITTED) { throw new IllegalStateException("The transaction has already been committed"); } else if (status != Status.STATUS_ACTIVE) { throw new IllegalStateException("The transaction is not active: " + status); } // mark for rollback txnInfo.getTransactionStatus().setRollbackOnly(); // make sure that we record the fact that we have been marked for rollback internalStatus = Status.STATUS_MARKED_ROLLBACK; // done if (logger.isDebugEnabled()) { logger.debug("Set transaction status to rollback only: " + this); } }
public void testNoTxnStatus() throws Exception { checkNoStatusOnThread(); assertEquals("Transaction status is not correct", Status.STATUS_NO_TRANSACTION, txn.getStatus()); assertEquals("Transaction manager not set up correctly", txn.getStatus(), transactionManager.getStatus()); }
public void testSimpleTxnWithCommit() throws Throwable { testNoTxnStatus(); try { txn.begin(); assertEquals("Transaction status is not correct", Status.STATUS_ACTIVE, txn.getStatus()); assertEquals("Transaction manager not called correctly", txn.getStatus(), transactionManager.getStatus()); txn.commit(); assertEquals("Transaction status is not correct", Status.STATUS_COMMITTED, txn.getStatus()); assertEquals("Transaction manager not called correctly", txn.getStatus(), transactionManager.getStatus()); } catch (Throwable e) { // unexpected exception - attempt a cleanup try { txn.rollback(); } catch (Throwable ee) { e.printStackTrace(); } throw e; } checkNoStatusOnThread(); }
public void commit() { status = Status.STATUS_NO_TRANSACTION; stubCalls.add("commit()"); if (failCommit) { throw new RollbackException(); } }