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

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

Introduction

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

Prototype

public static boolean hasResource(Object key) 

Source Link

Document

Check if there is a resource for the given key bound to the current thread.

Usage

From source file:ome.tools.hibernate.SessionStatus.java

private Session nullOrSessionBoundToThread() {
    SessionHolder holder = null;/*w  w  w  .  j av  a  2 s.  c  om*/
    if (TransactionSynchronizationManager.hasResource(factory)) {
        holder = (SessionHolder) TransactionSynchronizationManager.getResource(factory);
        // A bit tricky. Works in coordinate with resetThreadSession
        // since the DUMMY would be replaced anyway.
        if (holder != null && holder.isEmpty()) {
            holder = null;
        }
    }
    return holder == null ? null : holder.getSession();
}

From source file:org.broadleafcommerce.common.util.tenant.IdentityExecutionUtils.java

private static void finalizeTransaction(PlatformTransactionManager transactionManager,
        TransactionContainer container, boolean error) {
    TransactionUtils.finalizeTransaction(container.status, transactionManager, error);
    for (Map.Entry<Object, Object> entry : container.usedResources.entrySet()) {
        if (!TransactionSynchronizationManager.hasResource(entry.getKey())) {
            TransactionSynchronizationManager.bindResource(entry.getKey(), entry.getValue());
        }//w  w  w.  j a  v  a  2 s.co  m
    }
}

From source file:org.broadleafcommerce.common.util.tenant.IdentityExecutionUtils.java

private static TransactionContainer establishTransaction(PlatformTransactionManager transactionManager) {
    Map<Object, Object> usedResources = new HashMap<Object, Object>();
    Map<Object, Object> resources = TransactionSynchronizationManager.getResourceMap();
    for (Map.Entry<Object, Object> entry : resources.entrySet()) {
        if ((entry.getKey() instanceof EntityManagerFactory || entry.getKey() instanceof DataSource)
                && TransactionSynchronizationManager.hasResource(entry.getKey())) {
            usedResources.put(entry.getKey(), entry.getValue());
        }/*from  w ww . ja va2 s.  c o m*/
    }
    for (Map.Entry<Object, Object> entry : usedResources.entrySet()) {
        TransactionSynchronizationManager.unbindResource(entry.getKey());
    }

    TransactionStatus status;
    try {
        status = TransactionUtils.createTransaction(TransactionDefinition.PROPAGATION_REQUIRES_NEW,
                transactionManager, false);
    } catch (RuntimeException e) {
        throw e;
    }
    return new TransactionContainer(status, usedResources);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateTemplate.java

protected boolean isCurrentTransactionReadOnly() {
    if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            return TransactionSynchronizationManager.isCurrentTransactionReadOnly();
        } else {//from  www .ja  va  2 s .  com
            return osivReadOnly;
        }
    } else {
        return false;
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.support.HibernatePersistenceContextInterceptor.java

public void init() {
    if (incNestingCount() > 1) {
        return;/*w w w.  j  av  a  2s.co  m*/
    }
    SessionFactory sf = getSessionFactory();
    if (TransactionSynchronizationManager.hasResource(sf)) {
        // Do not modify the Session: just set the participate flag.
        setParticipate(true);
    } else {
        setParticipate(false);
        LOG.debug("Opening single Hibernate session in HibernatePersistenceContextInterceptor");
        Session session = getSession();
        GrailsHibernateUtil.enableDynamicFilterEnablerIfPresent(sf, session);
        session.setFlushMode(FlushMode.AUTO);
        TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
    }
}

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

public void init() {
    if (incNestingCount() > 1) {
        return;/*www  . j a  va  2 s . c  o  m*/
    }
    SessionFactory sf = getSessionFactory();
    if (sf == null) {
        return;
    }
    if (TransactionSynchronizationManager.hasResource(sf)) {
        // Do not modify the Session: just set the participate flag.
        setParticipate(true);
    } else {
        setParticipate(false);
        LOG.debug("Opening single Hibernate session in HibernatePersistenceContextInterceptor");
        Session session = getSession();
        GrailsHibernateUtil.enableDynamicFilterEnablerIfPresent(sf, session);
        session.setFlushMode(FlushMode.AUTO);
        TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
    }
}

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

public void init() {
    if (incNestingCount() > 1) {
        return;//from w  ww.  jav  a 2 s  .c om
    }
    SessionFactory sf = getSessionFactory();
    if (sf == null) {
        return;
    }
    if (TransactionSynchronizationManager.hasResource(sf)) {
        // Do not modify the Session: just set the participate flag.
        setParticipate(true);
    } else {
        setParticipate(false);
        LOG.debug("Opening single Hibernate session in HibernatePersistenceContextInterceptor");
        Session session = getSession();
        HibernateRuntimeUtils.enableDynamicFilterEnablerIfPresent(sf, session);
        session.setFlushMode(FlushMode.AUTO);
        TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
    }
}

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

private void runInSessionInternal(final SessionRunner r) throws Exception {
    boolean participate = false;
    try {/*w  w  w  .ja va2s . c  om*/

        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.service.impl.ScheduledTaskWrapperServiceImpl.java

protected Runnable withHibernateSession(final Runnable work) {
    return new Runnable() {
        @Override// w w  w. jav a2  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

public void openSession() {
    log.debug("HibernateContext: Opening Hibernate Session");
    if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
        if (log.isDebugEnabled()) {
            log.debug("Participating in existing session (" + sessionFactory.hashCode() + ")");
        }//from  w  w w  . j  a  va 2 s  .co  m
        participate = true;
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Registering session with synchronization manager (" + sessionFactory.hashCode() + ")");
        }
        Session session = sessionFactory.openSession();
        session.setFlushMode(FlushMode.MANUAL);
        TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
    }
}