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.iwethey.forums.db.test.HibernateTestHarness.java

public void tearDown() throws Exception {
    SessionFactory sf = (SessionFactory) this.context.getBean("sessionFactory");
    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
    Session s = holder.getSession();//from  w w  w  .  j  a  v a2  s .c  om
    s.flush();
    TransactionSynchronizationManager.unbindResource(sf);
    //SessionFactoryUtils.closeSessionIfNecessary(s, sf);
    SessionFactoryUtils.releaseSession(s, sf);
}

From source file:org.ops4j.orient.spring.tx.OrientTransactionManager.java

@Override
protected Object doGetTransaction() throws TransactionException {
    OrientTransaction tx = new OrientTransaction();

    ODatabaseInternal<?> db = (ODatabaseInternal<?>) TransactionSynchronizationManager
            .getResource(getResourceFactory());
    if (db != null) {
        tx.setDatabase(db);/*w  ww.  ja v  a2  s . c  o m*/
        tx.setTx(db.getTransaction());
    }

    return tx;
}

From source file:org.grails.datastore.mapping.core.AbstractAttributeStoringSession.java

/**
 * Performs clear up. Subclasses should always call into this super
 * implementation.//from   ww w. j  a  va 2s.c o  m
 */
public void disconnect() {
    connected = false;
    try {
        clear();
        attributes.clear();
    } finally {
        SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
                .getResource(getDatastore());
        if (sessionHolder != null) {
            sessionHolder.removeSession(this);
            if (sessionHolder.isEmpty()) {
                try {
                    TransactionSynchronizationManager.unbindResource(getDatastore());
                } catch (IllegalStateException e) {
                    // ignore session disconnected by a another thread
                }
            }
        }
    }
}

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

public Object getResource(String resourceName) {
    return TransactionSynchronizationManager.getResource(resourceName);
}

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

public void destroy() {
    if (participate) {
        return;// w  w  w.  j a  va  2 s  . co  m
    }

    // single session mode
    if (TransactionSynchronizationManager.getResource(datastore) != null) {
        SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.unbindResource(datastore);
        LOG.debug("Closing single Datastore session in DatastorePersistenceContextInterceptor");
        try {
            Session session = holder.getSession();
            DatastoreUtils.closeSession(session);
        } catch (RuntimeException ex) {
            LOG.error("Unexpected exception on closing Datastore Session", ex);
        }
    }
}

From source file:org.iwethey.forums.db.test.HibernateTestHarness.java

public Session getSession() {
    SessionFactory sf = (SessionFactory) this.context.getBean("sessionFactory");
    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
    return holder.getSession();
}

From source file:org.grails.datastore.mapping.web.support.OpenSessionInViewInterceptor.java

public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {
    // Only potentially flush in single session mode.
    if (!hasSessionBound()) {
        return;/* www  .j a v a2  s.co  m*/
    }

    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(getDatastore());
    LOG.debug("Flushing single Datastore Session in OpenSessionInViewInterceptor");
    final Session session = sessionHolder.getSession();

    if (session.getFlushMode() == FlushModeType.AUTO) {
        session.flush();
    }
}

From source file:org.codehaus.groovy.grails.plugins.springsecurity.GrailsWebApplicationObjectSupport.java

/**
 * Set up hibernate session./*from  www  . j a  v  a2 s  .  c  o  m*/
 * @return  the session container, which holds the session and a boolean indicating if the session was pre-existing
 */
protected SessionContainer setUpSession() {
    SessionFactory sessionFactory = getSessionFactory();

    Session session;
    boolean existing;
    if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
        logger.debug("Session already has transaction attached");
        existing = true;
        session = ((SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory)).getSession();
    } else {
        logger.debug("Session does not have transaction attached... Creating new one");
        existing = false;
        session = SessionFactoryUtils.getSession(sessionFactory, true);
        SessionHolder sessionHolder = new SessionHolder(session);
        TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);
    }

    return new SessionContainer(session, existing);
}

From source file:org.grails.datastore.mapping.transactions.DatastoreTransactionManager.java

@Override
protected Object doGetTransaction() throws TransactionException {
    TransactionObject txObject = new TransactionObject();

    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(getDatastore());
    if (sessionHolder != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Found thread-bound Session [" + sessionHolder.getSession()
                    + "] for Datastore transaction");
        }//from w ww .  ja  v  a  2  s.  c om
        txObject.setSessionHolder(sessionHolder);
    } else if (datastoreManagedSession) {
        try {
            Session session = getDatastore().getCurrentSession();
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "Found Datastore-managed Session [" + session + "] for Spring-managed transaction");
            }
            txObject.setExistingSession(session);
        } catch (ConnectionNotFoundException ex) {
            throw new DataAccessResourceFailureException(
                    "Could not obtain Datastore-managed Session for Spring-managed transaction", ex);
        }
    } else {
        Session session = getDatastore().connect();
        txObject.setSession(session);
    }

    return txObject;
}

From source file:org.drools.container.spring.beans.persistence.DroolsSpringJpaManager.java

public PersistenceContext getApplicationScopedPersistenceContext() {
    if (this.appScopedEntityManager == null) {
        // Use the App scoped EntityManager if the user has provided it, and it is open.
        this.appScopedEntityManager = (EntityManager) this.env.get(EnvironmentName.APP_SCOPED_ENTITY_MANAGER);
        if (this.appScopedEntityManager != null && !this.appScopedEntityManager.isOpen()) {
            throw new RuntimeException("Provided APP_SCOPED_ENTITY_MANAGER is not open");
        }/* w w w.j a  v  a 2 s  .com*/

        if (this.appScopedEntityManager == null) {
            EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager
                    .getResource(this.emf);
            if (emHolder == null) {
                this.appScopedEntityManager = this.emf.createEntityManager();
                emHolder = new EntityManagerHolder(this.appScopedEntityManager);
                TransactionSynchronizationManager.bindResource(this.emf, emHolder);
                internalAppScopedEntityManager = true;
            } else {
                this.appScopedEntityManager = emHolder.getEntityManager();
            }

            this.env.set(EnvironmentName.APP_SCOPED_ENTITY_MANAGER, emHolder.getEntityManager());
        }
    }
    if (TransactionSynchronizationManager.isActualTransactionActive()) {
        this.appScopedEntityManager.joinTransaction();
    }
    return new JpaPersistenceContext(this.appScopedEntityManager);
}