Java Code Examples for org.springframework.transaction.support.DefaultTransactionStatus

The following examples show how to use org.springframework.transaction.support.DefaultTransactionStatus. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
protected void doCommit(DefaultTransactionStatus status) {
	JcrTransactionObject txObject = (JcrTransactionObject) status
			.getTransaction();
	if (status.isDebug()) {
		LOG.debug("Committing JCR transaction on session ["
				+ txObject.getSessionHolder().getSession() + "]");
	}
	try {
		txObject.getSessionHolder().getTransaction().commit();
	} catch (Exception ex) {
		// assumably from commit call to the underlying JCR repository
		throw new TransactionSystemException(
				"Could not commit JCR transaction", ex);
	}
}
 
Example 2
Source Project: spring-analysis-note   Source File: TransactionSupportTests.java    License: MIT License 6 votes vote down vote up
@Test
public void existingTransaction() {
	PlatformTransactionManager tm = new TestTransactionManager(true, true);
	DefaultTransactionStatus status1 = (DefaultTransactionStatus)
			tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS));
	assertTrue("Must have transaction", status1.getTransaction() != null);
	assertTrue("Must not be new transaction", !status1.isNewTransaction());

	DefaultTransactionStatus status2 = (DefaultTransactionStatus)
			tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
	assertTrue("Must have transaction", status2.getTransaction() != null);
	assertTrue("Must not be new transaction", !status2.isNewTransaction());

	try {
		DefaultTransactionStatus status3 = (DefaultTransactionStatus)
				tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY));
		assertTrue("Must have transaction", status3.getTransaction() != null);
		assertTrue("Must not be new transaction", !status3.isNewTransaction());
	}
	catch (NoTransactionException ex) {
		fail("Should not have thrown NoTransactionException");
	}
}
 
Example 3
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JcrTransactionObject txObject = (JcrTransactionObject) status
			.getTransaction();
	if (status.isDebug()) {
		LOG.debug("Rolling back JCR transaction on session ["
				+ txObject.getSessionHolder().getSession() + "]");
	}
	try {
		txObject.getSessionHolder().getTransaction().rollback();
	} catch (Exception ex) {
		throw new TransactionSystemException(
				"Could not roll back JCR transaction", ex);
	} finally {
		if (!txObject.isNewSessionHolder()) {
			// Clear all pending inserts/updates/deletes in the Session.
			// Necessary for pre-bound Sessions, to avoid inconsistent
			// state.
			try {
				txObject.getSessionHolder().getSession().refresh(false);
			} catch (RepositoryException e) {
				// we already throw an exception (hold back this one).
			}
		}
	}
}
 
Example 4
Source Project: spring-analysis-note   Source File: JmsTransactionManager.java    License: MIT License 6 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JmsTransactionObject txObject = (JmsTransactionObject) status.getTransaction();
	Session session = txObject.getResourceHolder().getOriginalSession();
	if (session != null) {
		try {
			if (status.isDebug()) {
				logger.debug("Rolling back JMS transaction on Session [" + session + "]");
			}
			session.rollback();
		}
		catch (JMSException ex) {
			throw new TransactionSystemException("Could not roll back JMS transaction", ex);
		}
	}
}
 
Example 5
Source Project: java-technology-stack   Source File: JpaTransactionManager.java    License: MIT License 6 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Rolling back JPA transaction on EntityManager [" +
				txObject.getEntityManagerHolder().getEntityManager() + "]");
	}
	try {
		EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
		if (tx.isActive()) {
			tx.rollback();
		}
	}
	catch (PersistenceException ex) {
		throw new TransactionSystemException("Could not roll back JPA transaction", ex);
	}
	finally {
		if (!txObject.isNewEntityManagerHolder()) {
			// Clear all pending inserts/updates/deletes in the EntityManager.
			// Necessary for pre-bound EntityManagers, to avoid inconsistent state.
			txObject.getEntityManagerHolder().getEntityManager().clear();
		}
	}
}
 
Example 6
Source Project: java-technology-stack   Source File: TransactionSupportTests.java    License: MIT License 6 votes vote down vote up
@Test
public void existingTransaction() {
	PlatformTransactionManager tm = new TestTransactionManager(true, true);
	DefaultTransactionStatus status1 = (DefaultTransactionStatus)
			tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS));
	assertTrue("Must have transaction", status1.getTransaction() != null);
	assertTrue("Must not be new transaction", !status1.isNewTransaction());

	DefaultTransactionStatus status2 = (DefaultTransactionStatus)
			tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
	assertTrue("Must have transaction", status2.getTransaction() != null);
	assertTrue("Must not be new transaction", !status2.isNewTransaction());

	try {
		DefaultTransactionStatus status3 = (DefaultTransactionStatus)
				tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY));
		assertTrue("Must have transaction", status3.getTransaction() != null);
		assertTrue("Must not be new transaction", !status3.isNewTransaction());
	}
	catch (NoTransactionException ex) {
		fail("Should not have thrown NoTransactionException");
	}
}
 
Example 7
Source Project: java-technology-stack   Source File: JmsTransactionManager.java    License: MIT License 6 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JmsTransactionObject txObject = (JmsTransactionObject) status.getTransaction();
	Session session = txObject.getResourceHolder().getSession();
	if (session != null) {
		try {
			if (status.isDebug()) {
				logger.debug("Rolling back JMS transaction on Session [" + session + "]");
			}
			session.rollback();
		}
		catch (JMSException ex) {
			throw new TransactionSystemException("Could not roll back JMS transaction", ex);
		}
	}
}
 
Example 8
Source Project: lams   Source File: JpaTransactionManager.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Rolling back JPA transaction on EntityManager [" +
				txObject.getEntityManagerHolder().getEntityManager() + "]");
	}
	try {
		EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
		if (tx.isActive()) {
			tx.rollback();
		}
	}
	catch (PersistenceException ex) {
		throw new TransactionSystemException("Could not roll back JPA transaction", ex);
	}
	finally {
		if (!txObject.isNewEntityManagerHolder()) {
			// Clear all pending inserts/updates/deletes in the EntityManager.
			// Necessary for pre-bound EntityManagers, to avoid inconsistent state.
			txObject.getEntityManagerHolder().getEntityManager().clear();
		}
	}
}
 
Example 9
Source Project: lams   Source File: JdoTransactionManager.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void doCommit(DefaultTransactionStatus status) {
	JdoTransactionObject txObject = (JdoTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Committing JDO transaction on PersistenceManager [" +
				txObject.getPersistenceManagerHolder().getPersistenceManager() + "]");
	}
	try {
		Transaction tx = txObject.getPersistenceManagerHolder().getPersistenceManager().currentTransaction();
		tx.commit();
	}
	catch (JDOException ex) {
		// Assumably failed to flush changes to database.
		throw convertJdoAccessException(ex);
	}
}
 
Example 10
Source Project: lams   Source File: JdoTransactionManager.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JdoTransactionObject txObject = (JdoTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Rolling back JDO transaction on PersistenceManager [" +
				txObject.getPersistenceManagerHolder().getPersistenceManager() + "]");
	}
	try {
		Transaction tx = txObject.getPersistenceManagerHolder().getPersistenceManager().currentTransaction();
		if (tx.isActive()) {
			tx.rollback();
		}
	}
	catch (JDOException ex) {
		throw new TransactionSystemException("Could not roll back JDO transaction", ex);
	}
}
 
Example 11
@Override
protected void prepareForCommit(DefaultTransactionStatus status) {
	if (this.earlyFlushBeforeCommit && status.isNewTransaction()) {
		HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction();
		Session session = txObject.getSessionHolder().getSession();
		if (!session.getFlushMode().lessThan(FlushMode.COMMIT)) {
			logger.debug("Performing an early flush for Hibernate transaction");
			try {
				session.flush();
			}
			catch (HibernateException ex) {
				throw convertHibernateAccessException(ex);
			}
			finally {
				session.setFlushMode(FlushMode.MANUAL);
			}
		}
	}
}
 
Example 12
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Rolling back JPA transaction on EntityManager [" +
				txObject.getEntityManagerHolder().getEntityManager() + "]");
	}
	try {
		EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
		if (tx.isActive()) {
			tx.rollback();
		}
	}
	catch (PersistenceException ex) {
		throw new TransactionSystemException("Could not roll back JPA transaction", ex);
	}
	finally {
		if (!txObject.isNewEntityManagerHolder()) {
			// Clear all pending inserts/updates/deletes in the EntityManager.
			// Necessary for pre-bound EntityManagers, to avoid inconsistent state.
			txObject.getEntityManagerHolder().getEntityManager().clear();
		}
	}
}
 
Example 13
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JdoTransactionObject txObject = (JdoTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Rolling back JDO transaction on PersistenceManager [" +
				txObject.getPersistenceManagerHolder().getPersistenceManager() + "]");
	}
	try {
		Transaction tx = txObject.getPersistenceManagerHolder().getPersistenceManager().currentTransaction();
		if (tx.isActive()) {
			tx.rollback();
		}
	}
	catch (JDOException ex) {
		throw new TransactionSystemException("Could not roll back JDO transaction", ex);
	}
}
 
Example 14
@Override
protected void prepareForCommit(DefaultTransactionStatus status) {
	if (this.earlyFlushBeforeCommit && status.isNewTransaction()) {
		HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction();
		Session session = txObject.getSessionHolder().getSession();
		if (!session.getFlushMode().lessThan(FlushMode.COMMIT)) {
			logger.debug("Performing an early flush for Hibernate transaction");
			try {
				session.flush();
			}
			catch (HibernateException ex) {
				throw convertHibernateAccessException(ex);
			}
			finally {
				session.setFlushMode(FlushMode.MANUAL);
			}
		}
	}
}
 
Example 15
@Test
public void existingTransaction() {
	PlatformTransactionManager tm = new TestTransactionManager(true, true);
	DefaultTransactionStatus status1 = (DefaultTransactionStatus)
			tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS));
	assertTrue("Must have transaction", status1.getTransaction() != null);
	assertTrue("Must not be new transaction", !status1.isNewTransaction());

	DefaultTransactionStatus status2 = (DefaultTransactionStatus)
			tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
	assertTrue("Must have transaction", status2.getTransaction() != null);
	assertTrue("Must not be new transaction", !status2.isNewTransaction());

	try {
		DefaultTransactionStatus status3 = (DefaultTransactionStatus)
				tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY));
		assertTrue("Must have transaction", status3.getTransaction() != null);
		assertTrue("Must not be new transaction", !status3.isNewTransaction());
	}
	catch (NoTransactionException ex) {
		fail("Should not have thrown NoTransactionException");
	}
}
 
Example 16
Source Project: sdn-rx   Source File: Neo4jTransactionManager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void doCommit(DefaultTransactionStatus status) throws TransactionException {

	Neo4jTransactionObject transactionObject = extractNeo4jTransaction(status);
	Neo4jTransactionHolder transactionHolder = transactionObject.getRequiredResourceHolder();
	Bookmark lastBookmark = transactionHolder.commit();
	this.bookmarkManager.updateBookmarks(transactionHolder.getBookmarks(), lastBookmark);
}
 
Example 17
@Override
protected void doRollback(DefaultTransactionStatus status) {
	DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction();
	Connection con = txObject.getConnectionHolder().getConnection();
	if (status.isDebug()) {
		logger.debug("Rolling back JDBC transaction on Connection [" + con + "]");
	}
	try {
		con.rollback();
	}
	catch (SQLException ex) {
		throw new TransactionSystemException("Could not roll back JDBC transaction", ex);
	}
}
 
Example 18
Source Project: ignite   Source File: SpringTransactionManager.java    License: Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected void doRollback(DefaultTransactionStatus status) throws TransactionException {
    IgniteTransactionObject txObj = (IgniteTransactionObject)status.getTransaction();
    Transaction tx = txObj.getTransactionHolder().getTransaction();

    if (status.isDebug() && log.isDebugEnabled())
        log.debug("Rolling back Ignite transaction: " + tx);

    try {
        tx.rollback();
    }
    catch (IgniteException e) {
        throw new TransactionSystemException("Could not rollback Ignite transaction", e);
    }
}
 
Example 19
Source Project: spring-analysis-note   Source File: JpaTransactionManager.java    License: MIT License 5 votes vote down vote up
@Override
protected void doSetRollbackOnly(DefaultTransactionStatus status) {
	JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Setting JPA transaction on EntityManager [" +
				txObject.getEntityManagerHolder().getEntityManager() + "] rollback-only");
	}
	txObject.setRollbackOnly();
}
 
Example 20
@Override
protected void doCommit(DefaultTransactionStatus status) {
	if (!TRANSACTION.equals(status.getTransaction())) {
		throw new IllegalArgumentException("Not the same transaction object");
	}
	this.commit = true;
}
 
Example 21
@Override
public void run() {
	UOWManager uowManager = obtainUOWManager();
	DefaultTransactionStatus status = prepareTransactionStatus(
			this.definition, (this.actualTransaction ? this : null),
			this.newTransaction, this.newSynchronization, this.debug, null);
	try {
		this.result = this.callback.doInTransaction(status);
		triggerBeforeCommit(status);
	}
	catch (Throwable ex) {
		this.exception = ex;
		if (status.isDebug()) {
			logger.debug("Rolling back on application exception from transaction callback", ex);
		}
		uowManager.setRollbackOnly();
	}
	finally {
		if (status.isLocalRollbackOnly()) {
			if (status.isDebug()) {
				logger.debug("Transaction callback has explicitly requested rollback");
			}
			uowManager.setRollbackOnly();
		}
		triggerBeforeCompletion(status);
		if (status.isNewSynchronization()) {
			List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
			TransactionSynchronizationManager.clear();
			if (!synchronizations.isEmpty()) {
				uowManager.registerInterposedSynchronization(new JtaAfterCompletionSynchronization(synchronizations));
			}
		}
	}
}
 
Example 22
Source Project: spring-analysis-note   Source File: CciLocalTransactionManager.java    License: MIT License 5 votes vote down vote up
@Override
protected void doSetRollbackOnly(DefaultTransactionStatus status) {
	CciLocalTransactionObject txObject = (CciLocalTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Setting CCI local transaction [" + txObject.getConnectionHolder().getConnection() +
				"] rollback-only");
	}
	txObject.getConnectionHolder().setRollbackOnly();
}
 
Example 23
@Override
protected void doSetRollbackOnly(DefaultTransactionStatus status) {
	DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Setting JDBC transaction [" + txObject.getConnectionHolder().getConnection() +
				"] rollback-only");
	}
	txObject.setRollbackOnly();
}
 
Example 24
Source Project: spring-analysis-note   Source File: TestTransactionManager.java    License: MIT License 5 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) {
	if (!TRANSACTION.equals(status.getTransaction())) {
		throw new IllegalArgumentException("Not the same transaction object");
	}
	this.rollback = true;
}
 
Example 25
Source Project: spring-analysis-note   Source File: TestTransactionManager.java    License: MIT License 5 votes vote down vote up
@Override
protected void doSetRollbackOnly(DefaultTransactionStatus status) {
	if (!TRANSACTION.equals(status.getTransaction())) {
		throw new IllegalArgumentException("Not the same transaction object");
	}
	this.rollbackOnly = true;
}
 
Example 26
@Override
protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
	@SuppressWarnings("unchecked")
	List<DefaultTransactionStatus> list = 
	      (List<DefaultTransactionStatus>) status.getTransaction();

	logger.info("prepare to commit transactions on multiple data sources.");
	Validate.isTrue(list.size() <= this.getTransactionManagers().size());

	TransactionException lastException = null;
	for(int i=list.size()-1; i>=0;i--){
		PlatformTransactionManager transactionManager=this.getTransactionManagers().get(i);
		TransactionStatus localTransactionStatus=list.get(i);
		
		try{
			transactionManager.commit(localTransactionStatus);
		}
		catch (TransactionException e) {
			lastException=e;
			logger.error("Error in commit", e);

		}
	}
	if (lastException != null) {
		throw lastException;
		// Rollback will ensue as long as rollbackOnCommitFailure=true
	}

}
 
Example 27
protected void doCommit(DefaultTransactionStatus status) {

        ContextSourceAndHibernateTransactionObject actualTransactionObject = (ContextSourceAndHibernateTransactionObject) status
                .getTransaction();

        try {
            super.doCommit(new DefaultTransactionStatus(actualTransactionObject
                    .getHibernateTransactionObject(), status
                    .isNewTransaction(), status.isNewSynchronization(), status
                    .isReadOnly(), status.isDebug(), status
                    .getSuspendedResources()));
        } catch (TransactionException ex) {
            if (isRollbackOnCommitFailure()) {
                logger.debug("Failed to commit db resource, rethrowing", ex);
                // If we are to rollback on commit failure, just rethrow the
                // exception - this will cause a rollback to be performed on
                // both resources.
                throw ex;
            } else {
                logger
                        .warn("Failed to commit and resource is rollbackOnCommit not set -"
                                + " proceeding to commit ldap resource.");
            }
        }
        ldapManagerDelegate.doCommit(new DefaultTransactionStatus(
                actualTransactionObject.getLdapTransactionObject(), status
                        .isNewTransaction(), status.isNewSynchronization(),
                status.isReadOnly(), status.isDebug(), status
                        .getSuspendedResources()));
    }
 
Example 28
Source Project: java-technology-stack   Source File: JpaTransactionManager.java    License: MIT License 5 votes vote down vote up
@Override
protected void doSetRollbackOnly(DefaultTransactionStatus status) {
	JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Setting JPA transaction on EntityManager [" +
				txObject.getEntityManagerHolder().getEntityManager() + "] rollback-only");
	}
	txObject.setRollbackOnly();
}
 
Example 29
@Override
protected void doSetRollbackOnly(DefaultTransactionStatus status) {
	HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Setting Hibernate transaction on Session [" +
				txObject.getSessionHolder().getSession() + "] rollback-only");
	}
	txObject.setRollbackOnly();
}
 
Example 30
@Override
public void run() {
	UOWManager uowManager = obtainUOWManager();
	DefaultTransactionStatus status = prepareTransactionStatus(
			this.definition, (this.actualTransaction ? this : null),
			this.newTransaction, this.newSynchronization, this.debug, null);
	try {
		this.result = this.callback.doInTransaction(status);
		triggerBeforeCommit(status);
	}
	catch (Throwable ex) {
		this.exception = ex;
		if (status.isDebug()) {
			logger.debug("Rolling back on application exception from transaction callback", ex);
		}
		uowManager.setRollbackOnly();
	}
	finally {
		if (status.isLocalRollbackOnly()) {
			if (status.isDebug()) {
				logger.debug("Transaction callback has explicitly requested rollback");
			}
			uowManager.setRollbackOnly();
		}
		triggerBeforeCompletion(status);
		if (status.isNewSynchronization()) {
			List<TransactionSynchronization> synchronizations = TransactionSynchronizationManager.getSynchronizations();
			TransactionSynchronizationManager.clear();
			if (!synchronizations.isEmpty()) {
				uowManager.registerInterposedSynchronization(new JtaAfterCompletionSynchronization(synchronizations));
			}
		}
	}
}