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

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

Introduction

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

Prototype

public static void bindResource(Object key, Object value) throws IllegalStateException 

Source Link

Document

Bind the given resource for the given key 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  a2s  . 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./*w  w w .  ja v a  2s . c  om*/
 * @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:podd.dataaccess.hibernate.HibernateSessionHelper.java

public static void openSession(SessionFactory sessionFactory) {
    // To avoid LazyInitializationException       
    if (!TransactionSynchronizationManager.hasResource(sessionFactory)) {
        Session session = SessionFactoryUtils.getSession(sessionFactory, true);
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    }/*from  www  .  j  av a  2s.  c  o  m*/
}

From source file:com.rajesh.common.BaseHibernateTest.java

@Before
public void before() {

    SessionFactory sessionFactory = getSessionFactory();

    Session session = sessionFactory.openSession();
    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));

    tx = session.beginTransaction();//w  w w .  j av a  2  s .co  m
    LOG.debug("Opened HBN Session: " + session.getClass().getSimpleName());

}

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   ww w .j a v a 2  s  . com

    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

/**
 * @see ODatabaseInternal#begin()/*w w w .j  a va 2 s. c  om*/
 */
public void begin() {

    if (this.resource == null) {
        this.resource = this.database.getOrCreateConnection();
        TransactionSynchronizationManager.bindResource(this.database, this.resource);
    }
    this.resource.begin();
}

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

private void setUpTransactionManager() {
    Session session = getSessionFactory().openSession();
    TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session));
}

From source file:com.mobileman.filter.OpenSessionFilter.java

public static void setWebApplicationContext(WebApplicationContext webApplicationContext) {
    OpenSessionFilter.webApplicationContext = webApplicationContext;

    // initialize session...
    SessionFactory sessionFactory = lookupSessionFactory();

    if (sessionFactory != null) {
        if (isSingleSession()) {
            // single session mode
            if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
            } else {
                Session session = getSession(sessionFactory);
                TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
            }//  w w w  .j  a va2  s  .  c om
        } else {
            // deferred close mode
            if (SessionFactoryUtils.isDeferredCloseActive(sessionFactory)) {
            } else {
                SessionFactoryUtils.initDeferredClose(sessionFactory);
            }
        }
    }
}

From source file:com.healthcit.cacure.businessdelegates.export.DataExportTest.java

@Before
public void setUp() {
    EntityManager em = emf.createEntityManager();
    TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em));
}

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;/* w  ww  .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("*************************");
}