Search in sources :

Example 1 with DefaultTransactionStatus

use of org.springframework.transaction.support.DefaultTransactionStatus in project BroadleafCommerce by BroadleafCommerce.

the class TransactionLifecycleMonitor method finalizeTransaction.

protected void finalizeTransaction(TransactionLifecycleEvent event) {
    String finalizationType = event.getLifecycle().name();
    DefaultTransactionStatus status = (DefaultTransactionStatus) event.getParams()[0];
    EntityManager em = getEntityManagerFromTransactionObject(status.getTransaction());
    if (em != null) {
        Integer hashcode = em.hashCode();
        TransactionInfo info = infos.get(hashcode);
        if (info != null) {
            try {
                Throwable finalizationException = event.getException();
                if (finalizationException != null) {
                    info.setFaultStateDetected(true);
                    // An exception took place during finalization. Log our info to support for review.
                    StringWriter sw = new StringWriter();
                    finalizationException.printStackTrace(new PrintWriter(sw));
                    logger.support(String.format("TRANSACTIONMONITOR(3) - Exception during " + finalizationType + " finalization. Logging " + "TransactionInfo and finalization exception: \n%s finalizationStack=\'%s\'", info.toString(), sw.toString()));
                }
            } finally {
                infos.remove(hashcode);
            }
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StringWriter(java.io.StringWriter) DefaultTransactionStatus(org.springframework.transaction.support.DefaultTransactionStatus) PrintWriter(java.io.PrintWriter)

Example 2 with DefaultTransactionStatus

use of org.springframework.transaction.support.DefaultTransactionStatus in project molgenis by molgenis.

the class PostgreSqlTransactionManager method doCommit.

@Override
protected void doCommit(DefaultTransactionStatus status) {
    MolgenisTransaction transaction = (MolgenisTransaction) status.getTransaction();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Commit transaction [{}]", transaction.getId());
    }
    DefaultTransactionStatus jpaTransactionStatus = new DefaultTransactionStatus(transaction.getDataSourceTransaction(), status.isNewTransaction(), status.isNewSynchronization(), status.isReadOnly(), status.isDebug(), status.getSuspendedResources());
    if (!status.isReadOnly()) {
        transactionListeners.forEach(j -> j.commitTransaction(transaction.getId()));
    }
    try {
        super.doCommit(jpaTransactionStatus);
    } catch (TransactionException e) {
        throw translateTransactionException(e);
    }
    if (!status.isReadOnly()) {
        transactionListeners.forEach(j -> j.afterCommitTransaction(transaction.getId()));
    }
}
Also used : TransactionException(org.springframework.transaction.TransactionException) DefaultTransactionStatus(org.springframework.transaction.support.DefaultTransactionStatus)

Example 3 with DefaultTransactionStatus

use of org.springframework.transaction.support.DefaultTransactionStatus in project molgenis by molgenis.

the class PostgreSqlTransactionManager method doSetRollbackOnly.

@Override
protected void doSetRollbackOnly(DefaultTransactionStatus status) {
    MolgenisTransaction transaction = (MolgenisTransaction) status.getTransaction();
    DefaultTransactionStatus jpaTransactionStatus = new DefaultTransactionStatus(transaction.getDataSourceTransaction(), status.isNewTransaction(), status.isNewSynchronization(), status.isReadOnly(), status.isDebug(), status.getSuspendedResources());
    super.doSetRollbackOnly(jpaTransactionStatus);
}
Also used : DefaultTransactionStatus(org.springframework.transaction.support.DefaultTransactionStatus)

Example 4 with DefaultTransactionStatus

use of org.springframework.transaction.support.DefaultTransactionStatus in project spring-framework by spring-projects.

the class TransactionSupportTests method transactionTemplateWithRollbackException.

@SuppressWarnings("serial")
@Test
public void transactionTemplateWithRollbackException() {
    final TransactionSystemException tex = new TransactionSystemException("system exception");
    TestTransactionManager tm = new TestTransactionManager(false, true) {

        @Override
        protected void doRollback(DefaultTransactionStatus status) {
            super.doRollback(status);
            throw tex;
        }
    };
    TransactionTemplate template = new TransactionTemplate(tm);
    final RuntimeException ex = new RuntimeException("Some application exception");
    assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> template.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            throw ex;
        }
    })).isSameAs(tex);
    assertThat(tm.begin).as("triggered begin").isTrue();
    assertThat(tm.commit).as("no commit").isFalse();
    assertThat(tm.rollback).as("triggered rollback").isTrue();
    assertThat(tm.rollbackOnly).as("no rollbackOnly").isFalse();
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) DefaultTransactionStatus(org.springframework.transaction.support.DefaultTransactionStatus) DefaultTransactionStatus(org.springframework.transaction.support.DefaultTransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultTransactionStatus

use of org.springframework.transaction.support.DefaultTransactionStatus in project spring-framework by spring-projects.

the class TransactionSupportTests method existingTransaction.

@Test
public void existingTransaction() {
    PlatformTransactionManager tm = new TestTransactionManager(true, true);
    DefaultTransactionStatus status1 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS));
    assertThat(status1.getTransaction() != null).as("Must have transaction").isTrue();
    boolean condition2 = !status1.isNewTransaction();
    assertThat(condition2).as("Must not be new transaction").isTrue();
    DefaultTransactionStatus status2 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
    assertThat(status2.getTransaction() != null).as("Must have transaction").isTrue();
    boolean condition1 = !status2.isNewTransaction();
    assertThat(condition1).as("Must not be new transaction").isTrue();
    DefaultTransactionStatus status3 = (DefaultTransactionStatus) tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY));
    assertThat(status3.getTransaction() != null).as("Must have transaction").isTrue();
    boolean condition = !status3.isNewTransaction();
    assertThat(condition).as("Must not be new transaction").isTrue();
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) DefaultTransactionStatus(org.springframework.transaction.support.DefaultTransactionStatus) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultTransactionStatus (org.springframework.transaction.support.DefaultTransactionStatus)7 Test (org.junit.jupiter.api.Test)3 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 EntityManager (javax.persistence.EntityManager)1 TransactionException (org.springframework.transaction.TransactionException)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)1