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

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

Introduction

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

Prototype

@Nullable
public static Object getResource(Object key) 

Source Link

Document

Retrieve a resource for the given key that is bound to 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./*from   w  w w  .j av  a2 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.grails.datastore.mapping.transactions.TransactionUtils.java

public static Transaction<?> getTransaction(Datastore datastore) {
    final SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
            .getResource(datastore);/*from   w  ww. j  a v a2s.  co m*/
    return sessionHolder == null ? null : sessionHolder.getTransaction();
}

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.// w  w  w. j a  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:com.brienwheeler.lib.db.DbTestUtils.java

public static <T> T doInHibernateSession(ApplicationContext applicationContext, Callable<T> work) {
    EntityManagerFactory entityManagerFactory = applicationContext
            .getBean("com.brienwheeler.lib.db.appEntityManagerFactory", EntityManagerFactory.class);

    EntityManagerHolder entityManagerHolder = (EntityManagerHolder) TransactionSynchronizationManager
            .getResource(entityManagerFactory);

    boolean created = entityManagerHolder == null;
    if (created) {
        entityManagerHolder = new EntityManagerHolder(entityManagerFactory.createEntityManager());
        TransactionSynchronizationManager.bindResource(entityManagerFactory, entityManagerHolder);
    }/*from  www . j  a v a2  s  .c o m*/

    try {
        return work.call();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (created)
            TransactionSynchronizationManager.unbindResource(entityManagerFactory);
    }
}

From source file:net.sf.mmm.orient.db.impl.OrientTx.java

/**
 * The constructor./*from  w  w w  .  jav  a2s  .c o m*/
 *
 * @param database the {@link OrientDatabaseImpl} instance.
 */
public OrientTx(OrientDatabaseImpl database) {
    super();
    this.database = database;
    this.resource = (ODatabaseInternal<?>) TransactionSynchronizationManager.getResource(this.database);
}

From source file:hsa.awp.common.util.EntityManagerUtil.java

/**
 * Finds the current {@link EntityManager} which is bound to the current Thread. If there is no {@link EntityManager} mapped,
 * <code>null</code> will be returned.
 *
 * @return the bound {@link EntityManager} or <code>null</code> if no one can be found.
 *///w  w  w.j a  v a2  s .c  o  m
public static EntityManager getEntityManagerFromThread() {

    if (TransactionSynchronizationManager.hasResource(emf)) {
        EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.getResource(emf);
        return emHolder.getEntityManager();
    } else {
        return null;
    }
}

From source file:org.grails.datastore.gorm.support.DatastorePersistenceContextInterceptor.java

public void init() {
    if (TransactionSynchronizationManager.getResource(datastore) != null) {
        // Do not modify the Session: just set the participate flag.
        participate = true;//from www .jav a 2s . c  om
    } else {
        LOG.debug("Opening single Datastore session in DatastorePersistenceContextInterceptor");
        Session session = getSession();
        session.setFlushMode(FlushModeType.AUTO);
        try {
            DatastoreUtils.bindSession(session);
        } catch (IllegalStateException e) {
            // ignore, already bound
        }
    }
}

From source file:com.rainy.redis.connection.RedisConnectionUtils.java

/**
 * Gets a Redis connection. Is aware of and will return any existing
 * corresponding connections bound to the current thread, for example when
 * using a transaction manager. Will create a new Connection otherwise, if
 * {@code allowCreate} is <tt>true</tt>.
 * //  w w  w  .  java2  s . c o  m
 * @param factory connection factory for creating the connection
 * @param allowCreate whether a new (unbound) connection should be created
 *        when no connection can be found for the current thread
 * @param bind binds the connection to the thread, in case one was created
 * @return an active Redis connection
 */
public static IRedisConnection doGetConnection(IRedisConnectionFactory factory, boolean allowCreate,
        boolean bind) {
    Assert.notNull(factory, "No RedisConnectionFactory specified");

    RedisConnectionHolder connHolder = (RedisConnectionHolder) TransactionSynchronizationManager
            .getResource(factory);
    // TODO: investigate tx synchronization

    if (connHolder != null)
        return connHolder.getConnection();

    if (!allowCreate) {
        throw new IllegalArgumentException("No connection found and allowCreate = false");
    }

    if (log.isDebugEnabled())
        log.debug("Opening RedisConnection");

    IRedisConnection conn = factory.getConnection();

    if (bind) {
        connHolder = new RedisConnectionHolder(conn);
        TransactionSynchronizationManager.bindResource(factory, connHolder);
        return connHolder.getConnection();
    }
    return conn;
}

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  . j a v a2s . co 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();
    }
}

From source file:org.jbpm.instance.migration.HibernateTestSupport.java

public void tearDown() throws Exception {
    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(getSessionFactory());
    Session session = holder.getSession();
    TransactionSynchronizationManager.unbindResource(getSessionFactory());
    session.close();/*ww w  .  j  a v  a  2s .c o  m*/
}