Example usage for org.springframework.transaction.support TransactionSynchronizationManager isActualTransactionActive

List of usage examples for org.springframework.transaction.support TransactionSynchronizationManager isActualTransactionActive

Introduction

In this page you can find the example usage for org.springframework.transaction.support TransactionSynchronizationManager isActualTransactionActive.

Prototype

public static boolean isActualTransactionActive() 

Source Link

Document

Return whether there currently is an actual transaction active.

Usage

From source file:org.example.service.DepartmentServiceImpl.java

@Transactional
public List<Department> getAll() {
    System.out.println(TransactionSynchronizationManager.isActualTransactionActive());

    return departmentRepository.findAll();
}

From source file:org.csc.phynixx.spring.jta.JtaConnectionHolderSupportAspect.java

@Before("@annotation(org.csc.phynixx.spring.jta.JtaConnectionHolderSupport)")
public void before() throws Throwable {

    System.out.println("Actual Transaction Active for JtaConnectionHolderSynchronization"
            + TransactionSynchronizationManager.isActualTransactionActive());

    if (!TransactionSynchronizationManager.isActualTransactionActive()) {
        return;//from  ww  w  .  j  a  v a 2  s  .c o  m
    }
    Object connectionFactory = this.getConnectionFactory();
    if (TransactionSynchronizationManager.hasResource(connectionFactory)) {
        return;
    }
    ResourceHolder connectionHolder = this.getConnectionHolder();
    TransactionSynchronizationManager.bindResource(connectionFactory, connectionHolder);

    /**
     * support die LifeCycle of then binding
     */
    JtaConnectionHolderSynchronization<H, K> synchronization = new JtaConnectionHolderSynchronization(
            connectionHolder, connectionFactory);
    TransactionSynchronizationManager.registerSynchronization(synchronization);

    // Hole aktuelle Connection

    System.out.println("*************************");
}

From source file:org.apache.cayenne.access.SpringDataDomain.java

/**
 * Executes Transformer.transform() method in a transaction. Transaction
 * policy is to check for the thread transaction, and use it if one exists.
 * If it doesn't, a new transaction is created, with a scope limited to this
 * method.//from  w  ww . j ava2  s.co m
 */
@Override
Object runInTransaction(final Transformer operation) {
    if (!this.isUsingExternalTransactions()) {
        return super.runInTransaction(operation);
    } else {
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            return operation.transform(null);
        }
        TransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
        return transationOperation.execute(transactionDefinition, new TransactionCallback<Object>() {

            @Override
            public Object doInTransaction(TransactionStatus status) {
                return operation.transform(status);
            }
        });
    }
}

From source file:com.vladmihalcea.concurrent.aop.OptimisticConcurrencyControlAspect.java

private Object proceed(ProceedingJoinPoint pjp, Retry retryAnnotation) throws Throwable {
    int times = retryAnnotation.times();
    Class<? extends Throwable>[] retryOn = retryAnnotation.on();
    Assert.isTrue(times > 0, "@Retry{times} should be greater than 0!");
    Assert.isTrue(retryOn.length > 0, "@Retry{on} should have at least one Throwable!");
    if (retryAnnotation.failInTransaction() && TransactionSynchronizationManager.isActualTransactionActive()) {
        throw new IllegalTransactionStateException(
                "You shouldn't retry an operation from withing an existing Transaction."
                        + "This is because we can't retry if the current Transaction was already rollbacked!");
    }//from   ww  w .  ja  va 2  s. co m
    LOGGER.info("Proceed with {} retries on {}", times, Arrays.toString(retryOn));
    return tryProceeding(pjp, times, retryOn);
}

From source file:org.cfr.capsicum.core.CayenneTemplate.java

/**
 * Main worker method that wraps CayenneCalbac execution in Spring exception
 * handler./*from  w  ww.j  av  a  2 s .  c o m*/
 */
@Override
@SuppressWarnings("unchecked")
public <R> R execute(@Nonnull ICayenneCallback callback) throws DataAccessException {
    ObjectContext context = getObjectContext();
    try {
        R result = (R) callback.doInCayenne(context);
        if (!TransactionSynchronizationManager.isActualTransactionActive()) {
            context.commitChanges();
        }
        return result;
    } catch (CayenneRuntimeException ex) {
        if (!TransactionSynchronizationManager.isActualTransactionActive()) {
            rollbackDataContext(context, ex);
        }
        throw convertAccessException(ex);
    } catch (CayenneException ex) {
        if (!TransactionSynchronizationManager.isActualTransactionActive()) {
            rollbackDataContext(context, ex);
        }
        throw convertAccessException(ex);
    }
}

From source file:org.drools.container.spring.beans.persistence.DroolsSpringJpaManager.java

public PersistenceContext getApplicationScopedPersistenceContext() {
    if (this.appScopedEntityManager == null) {
        // Use the App scoped EntityManager if the user has provided it, and it is open.
        this.appScopedEntityManager = (EntityManager) this.env.get(EnvironmentName.APP_SCOPED_ENTITY_MANAGER);
        if (this.appScopedEntityManager != null && !this.appScopedEntityManager.isOpen()) {
            throw new RuntimeException("Provided APP_SCOPED_ENTITY_MANAGER is not open");
        }//from   ww  w .  jav a 2  s  . c o m

        if (this.appScopedEntityManager == null) {
            EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager
                    .getResource(this.emf);
            if (emHolder == null) {
                this.appScopedEntityManager = this.emf.createEntityManager();
                emHolder = new EntityManagerHolder(this.appScopedEntityManager);
                TransactionSynchronizationManager.bindResource(this.emf, emHolder);
                internalAppScopedEntityManager = true;
            } else {
                this.appScopedEntityManager = emHolder.getEntityManager();
            }

            this.env.set(EnvironmentName.APP_SCOPED_ENTITY_MANAGER, emHolder.getEntityManager());
        }
    }
    if (TransactionSynchronizationManager.isActualTransactionActive()) {
        this.appScopedEntityManager.joinTransaction();
    }
    return new JpaPersistenceContext(this.appScopedEntityManager);
}

From source file:org.drools.container.spring.beans.persistence.HumanTaskSpringTransactionManager.java

/**
 * Borrowed from Seam/*from   w ww.  java  2s  . c om*/
 */
public int getStatus() {
    if (ptm == null) {
        return TransactionManager.STATUS_NO_TRANSACTION;
    }

    // logger.debug( "Current TX name (According to TransactionSynchronizationManager) : " + TransactionSynchronizationManager.getCurrentTransactionName() );
    if (TransactionSynchronizationManager.isActualTransactionActive()) {
        TransactionStatus transaction = null;
        boolean commitNewTransaction = false;
        try {
            if (currentTransaction.size() == 0) {
                transaction = ptm.getTransaction(td);
                currentTransaction.push(transaction);
                commitNewTransaction = true;
                if (transaction.isNewTransaction()) {
                    return TransactionManager.STATUS_COMMITTED;
                }
            } else {
                transaction = currentTransaction.peek();
            }
            logger.debug("Current TX: " + transaction);
            // If SynchronizationManager thinks it has an active transaction but
            // our transaction is a new one
            // then we must be in the middle of committing
            if (transaction.isCompleted()) {
                if (transaction.isRollbackOnly()) {
                    return TransactionManager.STATUS_ROLLEDBACK;
                }
                return TransactionManager.STATUS_COMMITTED;
            } else {
                if (transaction.isRollbackOnly()) {
                    return 5;
                }
                return TransactionManager.STATUS_ACTIVE;
            }
        } finally {
            if (commitNewTransaction) {
                ptm.commit(transaction);
            }
        }
    }
    return TransactionManager.STATUS_NO_TRANSACTION;
}

From source file:org.drools.container.spring.beans.persistence.DroolsSpringTransactionManager.java

/**
 * Borrowed from Seam//  w  w  w.ja  v a 2 s.c o m
 */
public int getStatus() {
    if (ptm == null) {
        return TransactionManager.STATUS_NO_TRANSACTION;
    }

    logger.debug("Current TX name (According to TransactionSynchronizationManager) : "
            + TransactionSynchronizationManager.getCurrentTransactionName());
    if (TransactionSynchronizationManager.isActualTransactionActive()) {
        TransactionStatus transaction = null;
        try {
            if (currentTransaction == null) {
                transaction = ptm.getTransaction(td);
                if (transaction.isNewTransaction()) {
                    return TransactionManager.STATUS_COMMITTED;
                }
            } else {
                transaction = currentTransaction;
            }
            logger.debug("Current TX: " + transaction);
            // If SynchronizationManager thinks it has an active transaction but
            // our transaction is a new one
            // then we must be in the middle of committing
            if (transaction.isCompleted()) {
                if (transaction.isRollbackOnly()) {
                    return TransactionManager.STATUS_ROLLEDBACK;
                }
                return TransactionManager.STATUS_COMMITTED;
            } else {
                // Using the commented-out code in means that if rollback with this manager,
                //  I always have to catch and check the exception 
                //  because ROLLEDBACK can mean both "rolled back" and "rollback only".
                // if ( transaction.isRollbackOnly() ) {
                //     return TransactionManager.STATUS_ROLLEDBACK;
                // }

                return TransactionManager.STATUS_ACTIVE;
            }
        } finally {
            if (currentTransaction == null) {
                ptm.commit(transaction);
            }
        }
    }
    return TransactionManager.STATUS_NO_TRANSACTION;
}

From source file:org.grails.datastore.mapping.jpa.JpaSession.java

public static boolean hasTransaction() {
    return TransactionSynchronizationManager.isActualTransactionActive()
            && !TransactionSynchronizationManager.isCurrentTransactionReadOnly();
}

From source file:org.alfresco.util.transaction.TransactionSupportUtil.java

public static boolean isActualTransactionActive() {
    return TransactionSynchronizationManager.isActualTransactionActive();
}