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:ch.algotrader.dao.AbstractDaoTest.java

@Before
public void setup() throws Exception {

    SessionFactory sessionFactory = DATABASE.getSessionFactory();
    this.session = sessionFactory.openSession();

    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(this.session));

    this.dao = new GenericItemDao(sessionFactory);
}

From source file:hsa.awp.common.test.OpenEntityManagerInTest.java

/**
 * Opens an {@link EntityManager} per test.
 *///from w  ww  .  j  a va  2  s. c  om
@Before
public final void openEntityManager() {

    create = !TransactionSynchronizationManager.hasResource(emf);

    if (create) {
        log.debug("Opening EntityManager");
        em = emf.createEntityManager();

        log.trace("Binding EntityManager to thread '{}'", Thread.currentThread().getName());
        TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em));
    } else {
        log.debug("Don't open EntityManager because of @Transactional annotation");
    }
}

From source file:org.inbio.m3s.service.AbstractServiceTest.java

/**
 * One time setup which keeps a session open for the duration of the tests.
 *//*from  w w w.  j  ava2 s .  co  m*/
protected void onSetUp() throws Exception {
    //logger.trace("onSetup(): entered method.");

    SessionFactory sessionFactory = (SessionFactory) getBean("sessionFactory");
    Session session = sessionFactory.openSession();

    SessionHolder holder = new SessionHolder(session);
    TransactionSynchronizationManager.bindResource(sessionFactory, holder);
}

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

@Override
protected void doBegin(Object transaction, TransactionDefinition transactionDefinition)
        throws TransactionException {
    if (transactionDefinition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
        throw new InvalidIsolationLevelException("NeoDatis does not support an isolation level concept.");
    }/*from w  w w . j  av a  2  s  .  c o m*/

    ODB odb = null;

    try {
        NeoDatisTransactionObject transactionObject = (NeoDatisTransactionObject) transaction;
        if (transactionObject.getODBHolder() == null) {
            odb = getOdb();
            logger.debug("Using given ODB [{}] for the current thread transaction.", odb);
            transactionObject.setODBHolder(new ODBHolder(odb));
        }

        ODBHolder odbHolder = transactionObject.getODBHolder();

        odbHolder.setSynchronizedWithTransaction(true);

        // start transaction
        // no-op
        // register timeout
        if (transactionDefinition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
            transactionObject.getODBHolder().setTimeoutInSeconds(transactionDefinition.getTimeout());
        }

        //Bind session holder to the thread
        TransactionSynchronizationManager.bindResource(getOdb(), odbHolder);

    } catch (Exception e) {
        throw new CannotCreateTransactionException("Can not create an NeoDatis transaction.", e);
    }

}

From source file:org.motechproject.server.omod.sdsched.TxSyncManWrapperImplTest.java

public void testGetResource() {
    final String resourceName = "A Resource";
    final String resourceValue = "A Resource value";
    Object retVal = txTempl.execute(new TransactionCallback() {
        public Object doInTransaction(TransactionStatus status) {
            try {
                TransactionSynchronizationManager.bindResource(resourceName, resourceValue);
                return txSyncManWrapper.getResource(resourceName);
            } finally {
                TransactionSynchronizationManager.unbindResourceIfPossible(resourceName);
            }/*from   ww w .  j  ava  2s.c  o  m*/
        }
    });
    assertEquals(resourceValue, retVal);
}

From source file:hr.fer.zemris.vhdllab.dao.impl.support.AbstractDaoSupport.java

protected void createEntityManager() {
    entityManager = entityManagerFactory.createEntityManager();
    TransactionSynchronizationManager.bindResource(entityManagerFactory,
            new EntityManagerHolder(entityManager));
}

From source file:org.openvpms.component.business.dao.hibernate.im.common.Context.java

/**
 * Returns the context for the given assembler and session and current
 * thread.//from  w  w w  .  j  a  v  a 2 s.c om
 * <p/>
 * If one does not exist, it will be created.
 *
 * @param assembler the assembler
 * @param session   the hibernate session
 * @return the context
 */
public static Context getContext(Assembler assembler, Session session) {
    Context context;
    ResourceKey key = new ResourceKey(session);
    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        if (!TransactionSynchronizationManager.hasResource(key)) {
            context = new Context(assembler, session, true);
            TransactionSynchronizationManager.bindResource(context.getResourceKey(), context);
            TransactionSynchronizationManager.registerSynchronization(new ContextSynchronization(context));
        } else {
            context = (Context) TransactionSynchronizationManager.getResource(key);
        }
    } else {
        context = new Context(assembler, session, false);
    }
    return context;
}

From source file:ar.com.zauber.commons.repository.closures.OpenEntityManagerClosure.java

/** @see Closure#execute(Object) */
public final void execute(final T t) {
    if (dryrun) {
        //permite evitar que se hagan commit() en el ambiente de test
        target.execute(t);/*from   w  w w.j  a  v  a 2 s  .  com*/
    } else {
        boolean participate = false;
        EntityTransaction transaction = null;

        if (TransactionSynchronizationManager.hasResource(emf)) {
            // Do not modify the EntityManager: just set the participate flag.
            participate = true;
        } else {
            try {
                final EntityManager em = emf.createEntityManager();
                if (openTx) {
                    if (!warningPrinted) {
                        logger.warn(
                                "The OpenEntityManagerClosure has Transactions enabled and is not recommended"
                                        + ". This behaviour will change in the future. Check setOpenTx(), ");
                    }
                    transaction = em.getTransaction();
                    transaction.begin();
                }

                TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em));
            } catch (PersistenceException ex) {
                throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
            }
        }

        if (transaction != null) {
            try {
                target.execute(t);
                if (transaction.getRollbackOnly()) {
                    transaction.rollback();
                } else {
                    transaction.commit();
                }
            } catch (final Throwable e) {
                if (transaction != null && transaction.isActive()) {
                    transaction.rollback();
                }
                throw new UnhandledException(e);
            } finally {
                if (!participate) {
                    final EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager
                            .unbindResource(emf);
                    EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
                }
            }
        } else {
            try {
                target.execute(t);
            } finally {
                if (!participate) {
                    final EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager
                            .unbindResource(emf);
                    EntityManagerFactoryUtils.closeEntityManager(emHolder.getEntityManager());
                }
            }

        }
    }
}

From source file:org.tsm.concharto.service.KmlExportService.java

public void setupTransactionFactory() {
    Session session = SessionFactoryUtils.getSession(sessionFactory, auditInterceptor, null);
    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}

From source file:com.jaspersoft.jasperserver.test.CoreDataDeleteTestNG.java

@BeforeClass()
protected void onSetUp() throws Exception {
    m_logger.info("onSetUp() called");

    /*/*from  www  .j ava 2 s  . c  o m*/
     * The TransactionSynchronizationManager work is only needed in tests to allow multiple
     * Hibernate transactions to occur. Otherwise, each "template." call is a transaction.
     * Lazy initialization of collections will not occur otherwise. In a web app, there is Spring
     * configuration to do a transaction per web request - OpenSessionInViewFilter.
     */
    Session s = m_sessionFactory.openSession();
    TransactionSynchronizationManager.bindResource(m_sessionFactory, new SessionHolder(s));
    m_template = m_jasperServerDao.getHibernateTemplate();
}