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

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

Introduction

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

Prototype

public static Object unbindResource(Object key) throws IllegalStateException 

Source Link

Document

Unbind a resource for the given key from the current thread.

Usage

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

public void destroy() {
    // single session mode
    final SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
            .getResource(datastore);/*from   w w w . j av a 2 s  .co  m*/
    if (sessionHolder != null && this == sessionHolder.getCreator()) {
        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.grails.orm.hibernate.support.HibernatePersistenceContextInterceptor.java

public void destroy() {
    DeferredBindingActions.clear();//from  w  ww.j  av  a 2  s  .co  m
    if (getSessionFactory() == null || decNestingCount() > 0 || getParticipate()) {
        return;
    }

    try {
        // single session mode
        SessionHolder holder = (SessionHolder) TransactionSynchronizationManager
                .unbindResource(getSessionFactory());
        LOG.debug("Closing single Hibernate session in GrailsDispatcherServlet");
        try {
            SessionFactoryUtils.closeSession(holder.getSession());
        } catch (RuntimeException ex) {
            LOG.error("Unexpected exception on closing Hibernate Session", ex);
        }
    } finally {
        AbstractHibernateGormInstanceApi.clearDisabledValidations();
    }
}

From source file:org.grails.orm.hibernate3.support.HibernatePersistenceContextInterceptor.java

public void destroy() {
    DeferredBindingActions.clear();/*from  w  w  w.  java 2s.  com*/
    if (getSessionFactory() == null || decNestingCount() > 0 || getParticipate()) {
        return;
    }

    // single session mode
    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager
            .unbindResource(getSessionFactory());
    LOG.debug("Closing single Hibernate session in GrailsDispatcherServlet");
    try {
        Session session = holder.getSession();
        SessionFactoryUtils.closeSession(session);
    } catch (RuntimeException ex) {
        LOG.error("Unexpected exception on closing Hibernate Session", ex);
    }
}

From source file:org.grails.orm.hibernate4.support.HibernatePersistenceContextInterceptor.java

public void destroy() {
    DeferredBindingActions.clear();//w  ww  .  j  a v a 2 s.  co m
    if (!disconnected.isEmpty()) {
        disconnected.pop();
    }
    if (getSessionFactory() == null || decNestingCount() > 0 || getParticipate()) {
        return;
    }

    // single session mode
    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager
            .unbindResource(getSessionFactory());
    LOG.debug("Closing single Hibernate session in GrailsDispatcherServlet");
    try {
        disconnected.clear();
        SessionFactoryUtils.closeSession(holder.getSession());
    } catch (RuntimeException ex) {
        LOG.error("Unexpected exception on closing Hibernate Session", ex);
    }
}

From source file:org.hyperic.hq.hibernate.SessionManager.java

private void runInSessionInternal(final SessionRunner r) throws Exception {
    boolean participate = false;
    try {//from   w w w  .  j  ava 2  s .  c  o  m

        if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
            // Do not modify the Session: just set the participate flag.
            participate = true;
        } else {
            Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
            session.setFlushMode(FlushMode.MANUAL);
            TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session));
        }
        HibernateTemplate template = getHibernateTemplate();
        template.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                try {
                    r.run();
                } catch (Exception e) {
                    throw new HibernateException(e);
                }
                return null;
            }
        });
    } finally {
        if (!participate) {

            // single session mode
            SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
                    .unbindResource(getSessionFactory());
            SessionFactoryUtils.closeSession(sessionHolder.getSession());
        }
    }

}

From source file:org.jasig.ssp.dao.external.ExternalCourseDao.java

@Override
public void afterPropertiesSet() throws Exception {
    try {//from  w ww. j av  a 2s. c o  m
        Session session = sessionFactory.openSession();
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
        flushAndLoadCache();
        lastCacheFlush = Calendar.getInstance();
    } finally {
        SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
                .unbindResource(sessionFactory);
        SessionFactoryUtils.closeSession(sessionHolder.getSession());
    }
}

From source file:org.jasig.ssp.service.impl.ScheduledTaskWrapperServiceImpl.java

protected Runnable withHibernateSession(final Runnable work) {
    return new Runnable() {
        @Override/*ww  w .j  a v  a 2 s  . co  m*/
        public void run() {
            // Basically a copy/paste of Spring's
            // OpenSessionInViewFilter#doFilterInternal, with the
            // web-specific stuff removed
            boolean participate = false;
            try {
                if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
                    // Do not modify the Session: just set the participate flag.
                    LOGGER.debug("Scheduled task joining existing Hibernate session/transaction");
                    participate = true;
                } else {
                    LOGGER.debug("Scheduled task creating new Hibernate session");
                    Session session = sessionFactory.openSession();
                    session.setFlushMode(FlushMode.MANUAL);
                    SessionHolder sessionHolder = new SessionHolder(session);
                    TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);
                }

                work.run();

            } finally {
                if (!participate) {
                    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
                            .unbindResource(sessionFactory);
                    LOGGER.debug("Scheduled task closing Hibernate session");
                    SessionFactoryUtils.closeSession(sessionHolder.getSession());
                } else {
                    LOGGER.debug(
                            "Scheduled task joined existing Hibernate session/transaction so skipping that cleanup step");
                }
            }
        }
    };
}

From source file:org.openmrs.api.db.hibernate.HibernateContextDAO.java

/**
 * @see org.openmrs.api.context.Context#closeSession()
 *//*from www.  j a v  a2 s  .  c  o  m*/
public void closeSession() {
    log.debug("HibernateContext: closing Hibernate Session");
    if (!participate) {
        log.debug("Unbinding session from synchronization manager (" + sessionFactory.hashCode() + ")");

        if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
            Object value = TransactionSynchronizationManager.unbindResource(sessionFactory);
            try {
                if (value instanceof SessionHolder) {
                    Session session = ((SessionHolder) value).getSession();
                    SessionFactoryUtils.closeSession(session);
                }
            } catch (RuntimeException e) {
                log.error("Unexpected exception on closing Hibernate Session", e);
            }
        }
    } else {
        log.debug(
                "Participating in existing session, so not releasing session through synchronization manager");
    }
}

From source file:org.osaf.cosmo.hibernate.ThrowAwayHibernateSessionOnErrorInterceptor.java

private void handleException() {

    // If session is bound to transaction, close it and create/bind
    // new session to prevent stale data when retrying transaction
    if (TransactionSynchronizationManager.hasResource(sessionFactory)) {

        if (log.isDebugEnabled())
            log.debug("throwing away bad session and binding new one");

        // Get current session and close
        SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
                .unbindResource(sessionFactory);

        SessionFactoryUtils.closeSession(sessionHolder.getSession());

        // Open new session and bind (this session should be closed and
        // unbound elsewhere, for example OpenSessionInViewFilter)
        Session session = SessionFactoryUtils.getSession(sessionFactory, true);
        session.setFlushMode(FlushMode.MANUAL);
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    }//from  w  w w .j a  va 2  s.  co  m
}

From source file:org.osaf.cosmo.scheduler.HibernateSessionFilter.java

private void releaseSession() {
    log.debug("unbinding session to thread");

    // Unbind session from TransactionManager and close
    SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    Session s = holder.getSession();/*from  w w  w. j  a v  a2  s .com*/
    TransactionSynchronizationManager.unbindResource(sessionFactory);
    SessionFactoryUtils.closeSession(s);
}