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.springframework.orm.ojb.OjbFactoryUtils.java

/**
 * Get an OJB PersistenceBroker for the given PBKey. Is aware of a
 * corresponding PersistenceBroker bound to the current thread, for
 * example when using PersistenceBrokerTransactionManager. Will
 * create a new PersistenceBroker else, if allowCreate is true.
 * @param pbKey PBKey to create the PersistenceBroker for
 * @param allowCreate if a non-transactional PersistenceBroker should be created
 * when no transactional PersistenceBroker can be found for the current thread
 * @return the PersistenceBroker//from   w  w w  . j  a  v  a 2  s .  c  om
 * @throws DataAccessResourceFailureException if the PersistenceBroker couldn't be created
 * @throws IllegalStateException if no thread-bound PersistenceBroker found and allowCreate false
 */
public static PersistenceBroker getPersistenceBroker(PBKey pbKey, boolean allowCreate)
        throws DataAccessResourceFailureException, IllegalStateException {

    PersistenceBrokerHolder pbHolder = (PersistenceBrokerHolder) TransactionSynchronizationManager
            .getResource(pbKey);
    if (pbHolder != null) {
        return pbHolder.getPersistenceBroker();
    }

    if (!allowCreate && !TransactionSynchronizationManager.isSynchronizationActive()) {
        throw new IllegalStateException("No OJB PersistenceBroker bound to thread, "
                + "and configuration does not allow creation of non-transactional one here");
    }

    try {
        logger.debug("Opening OJB PersistenceBroker");
        PersistenceBroker pb = PersistenceBrokerFactory.createPersistenceBroker(pbKey);

        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            logger.debug("Registering transaction synchronization for OJB PersistenceBroker");
            // Use same PersistenceBroker for further OJB actions within the transaction.
            // Thread object will get removed by synchronization at transaction completion.
            pbHolder = new PersistenceBrokerHolder(pb);
            pbHolder.setSynchronizedWithTransaction(true);
            TransactionSynchronizationManager
                    .registerSynchronization(new PersistenceBrokerSynchronization(pbHolder, pbKey));
            TransactionSynchronizationManager.bindResource(pbKey, pbHolder);
        }

        return pb;
    } catch (OJBRuntimeException ex) {
        throw new DataAccessResourceFailureException("Could not open OJB PersistenceBroker", ex);
    }
}

From source file:org.springframework.orm.ojb.OjbFactoryUtils.java

/**
 * Close the given PersistenceBroker, created for the given PBKey,
 * if it is not managed externally (i.e. not bound to the thread).
 * @param pb PersistenceBroker to close/* www.  ja  v a 2s .  c o m*/
 * @param pbKey PBKey that the PersistenceBroker was created with
 */
public static void releasePersistenceBroker(PersistenceBroker pb, PBKey pbKey) {
    if (pb == null) {
        return;
    }

    PersistenceBrokerHolder pbHolder = (PersistenceBrokerHolder) TransactionSynchronizationManager
            .getResource(pbKey);
    if (pbHolder != null && pb == pbHolder.getPersistenceBroker()) {
        // It's the transactional PersistenceBroker: Don't close it.
        return;
    }

    logger.debug("Closing OJB PersistenceBroker");
    pb.close();
}

From source file:org.springframework.orm.toplink.SessionFactoryUtils.java

/**
 * Get a TopLink Session for the given SessionFactory. Is aware of and will
 * return any existing corresponding Session bound to the current thread, for
 * example when using TopLinkTransactionManager. Will create a new Session
 * otherwise, if "allowCreate" is <code>true</code>.
 * <p>Same as <code>getSession</code>, but throwing the original TopLinkException.
 * @param sessionFactory TopLink SessionFactory to create the session with
 * @param allowCreate if a non-transactional Session should be created when no
 * transactional Session can be found for the current thread
 * @return the TopLink Session/* w  w w . ja v  a2s  . c om*/
 * @throws TopLinkException if the Session couldn't be created
 * @throws IllegalStateException if no thread-bound Session found and
 * "allowCreate" is <code>false</code>
 * @see #releaseSession
 * @see TopLinkTemplate
 */
public static Session doGetSession(SessionFactory sessionFactory, boolean allowCreate)
        throws TopLinkException, IllegalStateException {

    Assert.notNull(sessionFactory, "No SessionFactory specified");

    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    if (sessionHolder != null) {
        return sessionHolder.getSession();
    }

    if (!allowCreate && !TransactionSynchronizationManager.isSynchronizationActive()) {
        throw new IllegalStateException("No TopLink Session bound to thread, "
                + "and configuration does not allow creation of non-transactional one here");
    }

    logger.debug("Creating TopLink Session");
    Session session = sessionFactory.createSession();

    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        logger.debug("Registering new Spring transaction synchronization for new TopLink Session");
        // Use same Session for further TopLink actions within the transaction.
        // Thread object will get removed by synchronization at transaction completion.
        sessionHolder = new SessionHolder(session);
        sessionHolder.setSynchronizedWithTransaction(true);
        TransactionSynchronizationManager
                .registerSynchronization(new SessionSynchronization(sessionHolder, sessionFactory));
        TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);
    }

    return session;
}

From source file:org.springframework.orm.toplink.SessionFactoryUtils.java

/**
 * Return whether the given TopLink Session is transactional, that is,
 * bound to the current thread by Spring's transaction facilities.
 * @param session the TopLink Session to check
 * @param sessionFactory TopLink SessionFactory that the Session was created with
 * (can be <code>null</code>)
 * @return whether the Session is transactional
 *///from   ww w.j a  va 2s. c  o  m
public static boolean isSessionTransactional(Session session, SessionFactory sessionFactory) {
    if (sessionFactory == null) {
        return false;
    }
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    return (sessionHolder != null && session == sessionHolder.getSession());
}

From source file:org.springframework.transaction.compensating.support.AbstractCompensatingTransactionManagerDelegate.java

public Object doGetTransaction() throws TransactionException {
    CompensatingTransactionHolderSupport holder = (CompensatingTransactionHolderSupport) TransactionSynchronizationManager
            .getResource(getTransactionSynchronizationKey());
    CompensatingTransactionObject txObject = new CompensatingTransactionObject(holder);
    return txObject;
}

From source file:org.springmodules.workflow.jbpm30.JbpmSessionFactoryUtils.java

/**
 * Returns a jBPM session. It is aware of and will return the thread-bound session if one is found.
 * jBPM exceptions will not be translated.
 * /*from  w w w  .j a v  a2 s  .c om*/
 * @param sessionFactory
 * @param allowCreate
 * @return
 */
public static JbpmSession doGetSession(JbpmSessionFactory sessionFactory, boolean allowCreate) {
    Assert.notNull(sessionFactory, "No JbpmSessionFactory specified");

    JbpmSessionHolder jbpmSessionHolder = (JbpmSessionHolder) TransactionSynchronizationManager
            .getResource(sessionFactory);

    if (jbpmSessionHolder != null && jbpmSessionHolder.getJbpmSession() != null) {
        return jbpmSessionHolder.getJbpmSession();
    }

    JbpmSession jbpmSession = sessionFactory.openJbpmSession();
    jbpmSessionHolder = new JbpmSessionHolder(jbpmSession);

    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        TransactionSynchronizationManager.registerSynchronization(
                new SpringJbpmSessionSynchronization(jbpmSessionHolder, sessionFactory));
        TransactionSynchronizationManager.bindResource(sessionFactory, jbpmSessionHolder);
    }

    return jbpmSession;
}

From source file:org.springmodules.workflow.jbpm30.JbpmSessionFactoryUtils.java

/**
 * Return whether the given jBPM Session is transactional, that is,
 * bound to the current thread by Spring's transaction facilities.
 * /* w w  w .ja v  a 2s.c  o  m*/
 * @param jbpmSession
 * @param jbpmSessionFactory
 * @return
 */
public static boolean isTransactional(JbpmSession jbpmSession, JbpmSessionFactory jbpmSessionFactory) {
    if (jbpmSessionFactory == null)
        return false;

    JbpmSessionHolder jbpmSessionHolder = (JbpmSessionHolder) TransactionSynchronizationManager
            .getResource(jbpmSessionFactory);
    return (jbpmSessionHolder != null && jbpmSessionHolder.getJbpmSession() == jbpmSession);
}

From source file:org.unitils.orm.hibernate.HibernateModule.java

@Override
protected Session doGetActivePersistenceContext(Object testObject) {
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
            .getResource(getPersistenceUnit(testObject));
    if (sessionHolder != null && sessionHolder.getSession() != null && sessionHolder.getSession().isOpen()) {
        return sessionHolder.getSession();
    }/*from  w w w.  j a  v  a  2 s  .  co  m*/
    return null;
}

From source file:org.unitils.orm.jpa.JpaModule.java

protected EntityManager doGetActivePersistenceContext(Object testObject) {
    EntityManagerHolder entityManagerHolder = (EntityManagerHolder) TransactionSynchronizationManager
            .getResource(getPersistenceUnit(testObject));
    if (entityManagerHolder != null && entityManagerHolder.getEntityManager() != null
            && entityManagerHolder.getEntityManager().isOpen()) {
        return entityManagerHolder.getEntityManager();
    }//  w  w w .  j  a  v a 2  s. co m
    return null;
}

From source file:test.eryansky.HibernateTest.java

@After
public void close() {
    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    SessionFactoryUtils.closeSession(holder.getSession());
    TransactionSynchronizationManager.unbindResource(sessionFactory);
}