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

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

Introduction

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

Prototype

public static boolean isSynchronizationActive() 

Source Link

Document

Return if transaction synchronization is active for the current thread.

Usage

From source file:org.skife.jdbi.v2.spring.DBIUtil.java

/**
 * Obtain a Handle instance, either the transactionally bound one if we are in a transaction,
 * or a new one otherwise.// w w w  .  j  a va2 s. c  o  m
 * @param dbi the IDBI instance from which to obtain the handle
 */
public static Handle getHandle(IDBI dbi) {
    Handle bound = (Handle) TransactionSynchronizationManager.getResource(dbi);
    if (bound == null) {
        bound = dbi.open();
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.bindResource(dbi, bound);
            TransactionSynchronizationManager.registerSynchronization(new Adapter(dbi, bound));
            TRANSACTIONAL_HANDLES.add(bound);
        }
    }
    return bound;
}

From source file:org.jdbi.v3.spring4.JdbiUtil.java

/**
 * Obtain a Handle instance, either the transactionally bound one if we are in a transaction,
 * or a new one otherwise.//from  www  .ja v a  2  s  .c o  m
 * @param jdbi the Jdbi instance from which to obtain the handle
 *
 * @return the Handle instance
 */
public static Handle getHandle(Jdbi jdbi) {
    Handle bound = (Handle) TransactionSynchronizationManager.getResource(jdbi);
    if (bound == null) {
        bound = jdbi.open();
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.bindResource(jdbi, bound);
            TransactionSynchronizationManager.registerSynchronization(new Adapter(jdbi, bound));
            TRANSACTIONAL_HANDLES.add(bound);
        }
    }
    return bound;
}

From source file:bigbank.transaction.DefaultSynchronizationManager.java

@Override
public boolean isSynchronizationActive() {
    return TransactionSynchronizationManager.isSynchronizationActive();
}

From source file:com.becool.base.spring.tx.SpringTransactionSynchronizationManager.java

public boolean isSynchronizationActive() {
    return TransactionSynchronizationManager.isSynchronizationActive();
}

From source file:org.brekka.commons.persistence.support.SpringTransactionedRetryAspect.java

@Around("@annotation(org.springframework.transaction.annotation.Transactional)")
public Object retryOnConcurrentError(ProceedingJoinPoint pjp) throws Throwable {
    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        // Already in a transaction
        return pjp.proceed();
    }/* w  ww .java2  s  . com*/
    return attemptWithRetry(pjp);
}

From source file:org.springextensions.neodatis.NeoDatisTransactionManagerTest.java

@Test
public void testTransactionCommit() throws Exception {
    final ODB odb = Mockito.mock(ODB.class);
    PlatformTransactionManager tm = new NeoDatisTransactionManager(odb);
    TransactionTemplate tmpl = new TransactionTemplate(tm);

    Assert.assertFalse("Should not have a resource", TransactionSynchronizationManager.hasResource(odb));
    Assert.assertFalse("There should no active synchronizations",
            TransactionSynchronizationManager.isSynchronizationActive());

    tmpl.execute(new TransactionCallbackWithoutResult() {
        @Override/*w  ww . j  a  va  2 s .c om*/
        protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
            Assert.assertTrue(TransactionSynchronizationManager.hasResource(odb));
            NeoDatisTemplate neoDatisTemplate = new NeoDatisTemplate(odb);
            neoDatisTemplate.store(null);
        }
    });

    Assert.assertFalse("Should not have a resource", TransactionSynchronizationManager.hasResource(odb));
    Assert.assertFalse("There should no active synchronizations",
            TransactionSynchronizationManager.isSynchronizationActive());

    Mockito.verify(odb, Mockito.times(1)).commit();

}

From source file:de.codecentric.batch.metrics.BatchMetricsImpl.java

@Override
public void increment(String metricName, Long value) {
    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        initializeMetricContainerAndRegisterTransactionSynchronizationIfNecessary();
        metricContainer.get().metrics.add(Pair.of(metricName, value));
    } else {/* w w  w . j  a  va 2 s  . c  o  m*/
        incrementNonTransactional(metricName, value);
    }
}

From source file:org.springextensions.db4o.Db4oTransactionManagerTest.java

@Test
public void testTransactionCommit() throws Exception {
    final ExtObjectContainer container = mock(ExtObjectContainer.class);
    when(container.identity()).thenReturn(null);

    PlatformTransactionManager tm = new Db4oTransactionManager(container);
    TransactionTemplate tt = new TransactionTemplate(tm);

    Assert.assertTrue(!TransactionSynchronizationManager.hasResource(container), "Has no container");
    Assert.assertTrue(!TransactionSynchronizationManager.isSynchronizationActive(),
            "JTA synchronizations not active");

    tt.execute(new TransactionCallbackWithoutResult() {
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            Assert.assertTrue(TransactionSynchronizationManager.hasResource(container), "Has thread session");
            Db4oTemplate template = new Db4oTemplate(container);
            template.identity();/* ww w  . j a v a 2  s.  c  o  m*/
        }
    });

    Assert.assertTrue(!TransactionSynchronizationManager.hasResource(container), "Has no container");
    Assert.assertTrue(!TransactionSynchronizationManager.isSynchronizationActive(),
            "JTA synchronizations not active");

    verify(container).commit();
}

From source file:cn.edu.ahpu.common.dao.key.impl.DefaultUniqueTableApp.java

public CacheValue getCacheValue(int cacheNum, String name) {
    Assert.isTrue(TransactionSynchronizationManager.isSynchronizationActive(), "Transaction must be running");

    CacheValue cache = null;/*from   w ww.j  a v  a 2  s  .c  o  m*/
    try {
        cache = getCurrCode(name);

        if (cache == null) {
            insert(name);
            cache = getCurrCode(name);
        }

        update(cacheNum, name);
        cache.setMaxVal(cache.getMinVal() + cacheNum);
    } catch (Exception e) {
        logger.error("?", e);
    }
    return cache;
}

From source file:com.github.rholder.spring.transaction.TransactionBindingSupport.java

/**
 * @return Returns the system time when the transaction started, or -1 if there is no current transaction.
 *///from w w  w  . ja v a  2 s . c  o m
public static long getTransactionStartTime() {
    /*
     * This method can be called outside of a transaction, so we can go direct to the synchronizations.
     */
    TransactionSynchronizationImpl txnSynch = (TransactionSynchronizationImpl) TransactionSynchronizationManager
            .getResource(RESOURCE_KEY_TXN_SYNCH);
    if (txnSynch == null) {
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            // need to lazily register synchronizations
            return registerSynchronizations().getTransactionStartTime();
        } else {
            return -1; // not in a transaction
        }
    } else {
        return txnSynch.getTransactionStartTime();
    }
}