org.springframework.transaction.support.DefaultTransactionStatus Java Examples

The following examples show how to use org.springframework.transaction.support.DefaultTransactionStatus. 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
Source File: TransactionSupportTests.java    From spring-analysis-note with 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 #2
Source File: JpaTransactionManager.java    From spring4-understanding with Apache License 2.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 #3
Source File: JdoTransactionManager.java    From spring4-understanding with Apache License 2.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 #4
Source File: LocalTransactionManager.java    From mycollab with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 #5
Source File: HibernateTransactionManager.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: TransactionSupportTests.java    From spring4-understanding with Apache License 2.0 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 File: LocalTransactionManager.java    From mycollab with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 #8
Source File: JmsTransactionManager.java    From spring-analysis-note with 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 #9
Source File: HibernateTransactionManager.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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 #10
Source File: JpaTransactionManager.java    From java-technology-stack with 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 #11
Source File: JdoTransactionManager.java    From lams with 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 #12
Source File: JdoTransactionManager.java    From lams with 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 #13
Source File: TransactionSupportTests.java    From java-technology-stack with 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 #14
Source File: JpaTransactionManager.java    From lams with 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 #15
Source File: JmsTransactionManager.java    From java-technology-stack with 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 #16
Source File: SpringTransactionManager.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
    IgniteTransactionObject txObj = (IgniteTransactionObject)status.getTransaction();
    Transaction tx = txObj.getTransactionHolder().getTransaction();

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

    try {
        tx.commit();
    }
    catch (IgniteException e) {
        throw new TransactionSystemException("Could not commit Ignite transaction", e);
    }
}
 
Example #17
Source File: SearchPlatformTransactionManager.java    From jkes with Apache License 2.0 5 votes vote down vote up
/**
 * Perform an actual commit of the given transaction.
 * <p>An implementation does not need to check the "new transaction" flag
 * or the rollback-only flag; this will already have been handled before.
 * Usually, a straight commit will be performed on the transaction object
 * contained in the passed-in status.
 * @param status the status representation of the transaction
 * @throws TransactionException in case of commit or system errors
 * @see DefaultTransactionStatus#getTransaction
 */
@Override
protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
    ReflectionUtils.invokeMethod(platformTransactionManager, "doCommit",
            new Class<?>[]{DefaultTransactionStatus.class},
            status);
    LOGGER.debug("finished committing transaction");

    LOGGER.debug("begin to handle event");
    eventSupport.handleAndClearEvent();
    LOGGER.debug("finished handling event");
}
 
Example #18
Source File: DataSourceTransactionManager.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
@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 #19
Source File: WebSphereUowTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
	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;
		uowManager.setRollbackOnly();
	}
	finally {
		if (status.isLocalRollbackOnly()) {
			if (status.isDebug()) {
				logger.debug("Transactional code has 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 #20
Source File: JpaTransactionManager.java    From lams with GNU General Public License v2.0 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 #21
Source File: CciLocalTransactionManager.java    From spring4-understanding with Apache License 2.0 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 #22
Source File: CciLocalTransactionManager.java    From lams with GNU General Public License v2.0 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
Source File: DataSourceTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 File: TestTransactionManager.java    From spring4-understanding with Apache License 2.0 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 #25
Source File: SpringTransactionManager.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected void doSetRollbackOnly(DefaultTransactionStatus status) throws TransactionException {
    IgniteTransactionObject txObj = (IgniteTransactionObject)status.getTransaction();
    Transaction tx = txObj.getTransactionHolder().getTransaction();

    assert tx != null;

    if (status.isDebug() && log.isDebugEnabled())
        log.debug("Setting Ignite transaction rollback-only: " + tx);

    tx.setRollbackOnly();
}
 
Example #26
Source File: MultipleDataSourcesTransactionManager.java    From cobarclient with Apache License 2.0 5 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) throws TransactionException {
	@SuppressWarnings("unchecked")
	List<DefaultTransactionStatus> list = 
	     (List<DefaultTransactionStatus>) status.getTransaction();

	logger.info("prepare to rollback 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.rollback(localTransactionStatus);
		} catch (TransactionException e) {
			// Log exception and try to complete rollback
			lastException = e;
			logger.error("error occured when rolling back the transaction. \n{}",e);
		}
	}
	
	if (lastException != null) {
		throw lastException;
	}
}
 
Example #27
Source File: StubPlatformTransactionManager.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Override
protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
	if (rollbackOnly) {
		doRollback(status);
		rollbackOnly = false;
	} else {
		commit++;
		total++;
	}
}
 
Example #28
Source File: Neo4jTransactionManager.java    From sdn-rx with 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 #29
Source File: KualiTransactionInterceptor.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
    * @see org.springframework.transaction.interceptor.TransactionAspectSupport#createTransactionIfNecessary(java.lang.reflect.Method,
    *      java.lang.Class)
    */
@Override
   protected TransactionInfo createTransactionIfNecessary(Method method, Class targetClass) {
       TransactionInfo txInfo = super.createTransactionIfNecessary(method, targetClass);

       // using INFO level since DEBUG level turns on the (somewhat misleading) log statements of the superclass
       if (logger.isDebugEnabled()) {
           if (txInfo != null) {
               TransactionStatus txStatus = txInfo.getTransactionStatus();
               if (txStatus != null) {
                   if (txStatus.isNewTransaction()) {
                       LOG.debug("creating explicit transaction for " + txInfo.getJoinpointIdentification());
                   }
                   else {
                       if (txStatus instanceof DefaultTransactionStatus) {
                           DefaultTransactionStatus dtxStatus = (DefaultTransactionStatus) txStatus;

                           if (dtxStatus.isNewSynchronization()) {
                               LOG.debug("creating implicit transaction for " + txInfo.getJoinpointIdentification());
                           }
                       }
                   }
               }
           }
       }

       return txInfo;
   }
 
Example #30
Source File: JdoTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void doSetRollbackOnly(DefaultTransactionStatus status) {
	JdoTransactionObject txObject = (JdoTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Setting JDO transaction on PersistenceManager [" +
				txObject.getPersistenceManagerHolder().getPersistenceManager() + "] rollback-only");
	}
	txObject.setRollbackOnly();
}