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:nl.strohalm.cyclos.struts.CyclosRequestProcessor.java

private SessionHolder getSessionHolder() {
    return (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
}

From source file:nl.strohalm.cyclos.struts.CyclosRequestProcessor.java

private void openReadOnlyConnection(final HttpServletRequest request) {
    if (noTransaction(request)) {
        return;//from  w w  w  .  jav a2s .  c  o m
    }
    logDebug(request, "Opening read-only transaction for include");

    final Connection connection = (Connection) TransactionSynchronizationManager
            .getResource(connectionProvider);

    final SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    final Session session = holder.getSession();
    session.setFlushMode(FlushMode.MANUAL);
    session.setDefaultReadOnly(true);
    session.reconnect(connection);

    TransactionSynchronizationManager.setCurrentTransactionReadOnly(true);
}

From source file:nl.strohalm.cyclos.struts.CyclosRequestProcessor.java

private void rollbackReadOnlyConnection(final HttpServletRequest request) {
    if (noTransaction(request)) {
        return;//ww  w  .  jav  a2s.  c o m
    }
    final Connection connection = (Connection) TransactionSynchronizationManager
            .getResource(connectionProvider);
    try {
        logDebug(request, "Rolling back read-only transaction");
        connection.rollback();
    } catch (final SQLException e) {
        throw new IllegalStateException(e);
    }
}

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

public void cleanThread() {
    if (TransactionSynchronizationManager.hasResource(factory)) {
        SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(factory);
        if (holder == null) {
            throw new IllegalStateException("Can't be null.");
        } else if (holder == DUMMY) {
            TransactionSynchronizationManager.unbindResource(factory);
        } else {/*from  w w  w .j av  a 2s  . c  o  m*/
            throw new IllegalStateException("Thread corrupted.");
        }
    }
}

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

private Session nullOrSessionBoundToThread() {
    SessionHolder holder = null;/*from  w  w  w .j  a  v  a2 s  .  c  o m*/
    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.codehaus.groovy.grails.orm.hibernate.GrailsHibernateTemplate.java

/**
 * Prepare the given Query object, applying cache settings and/or a
 * transaction timeout./*  ww  w  .  j  a va2  s. c o  m*/
 *
 * @param query the Query object to prepare
 * @see SessionFactoryUtils#applyTransactionTimeout
 */
protected void prepareQuery(Query query) {
    if (cacheQueries) {
        query.setCacheable(true);
    }
    if (isCurrentTransactionReadOnly()) {
        query.setReadOnly(true);
    }
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    if (sessionHolder != null && sessionHolder.hasTimeout()) {
        query.setTimeout(sessionHolder.getTimeToLiveInSeconds());
    }
}

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

/**
 * Prepare the given Criteria object, applying cache settings and/or a
 * transaction timeout./*  w  ww.  j  av a 2s  .c o  m*/
 *
 * @param criteria the Criteria object to prepare
 * @see SessionFactoryUtils#applyTransactionTimeout
 */
protected void prepareCriteria(Criteria criteria) {
    if (cacheQueries) {
        criteria.setCacheable(true);
    }
    if (isCurrentTransactionReadOnly()) {
        criteria.setReadOnly(true);
    }
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    if (sessionHolder != null && sessionHolder.hasTimeout()) {
        criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds());
    }
}

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

/**
 * Retrieve the Spring-managed Session for the current thread, if any.
 *//*from   w ww.j a  v  a2  s  .  c  o m*/
public Session currentSession() throws HibernateException {
    Object value = TransactionSynchronizationManager.getResource(sessionFactory);
    if (value instanceof Session) {
        return (Session) value;
    }

    if (value instanceof SessionHolder) {
        SessionHolder sessionHolder = (SessionHolder) value;
        Session session = sessionHolder.getSession();
        if (TransactionSynchronizationManager.isSynchronizationActive()
                && !sessionHolder.isSynchronizedWithTransaction()) {
            TransactionSynchronizationManager
                    .registerSynchronization(createSpringSessionSynchronization(sessionHolder));
            sessionHolder.setSynchronizedWithTransaction(true);
            // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
            // with FlushMode.MANUAL, which needs to allow flushing within the transaction.
            FlushMode flushMode = session.getFlushMode();
            if (FlushMode.isManualFlushMode(flushMode)
                    && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
                session.setFlushMode(FlushMode.AUTO);
                sessionHolder.setPreviousFlushMode(flushMode);
            }
        }
        return session;
    }

    if (jtaSessionContext != null) {
        Session session = jtaSessionContext.currentSession();
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager
                    .registerSynchronization(createSpringFlushSynchronization(session));
        }
        return session;
    }

    if (allowCreate) {
        // be consistent with older HibernateTemplate behavior
        return createSession(value);
    }

    throw new HibernateException("No Session found for current thread");
}

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

private Session getSession(boolean allowCreate) {

    Object value = TransactionSynchronizationManager.getResource(getSessionFactory());
    if (value instanceof Session) {
        return (Session) value;
    }//from w  w w  .j  a  va  2  s .  c  o m

    if (value instanceof SessionHolder) {
        SessionHolder sessionHolder = (SessionHolder) value;
        return sessionHolder.getSession();
    }

    if (allowCreate) {
        return getSessionFactory().openSession();
    }

    throw new IllegalStateException(
            "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here");
}

From source file:org.codehaus.groovy.grails.webflow.persistence.SessionAwareHibernateFlowExecutionListener.java

private void obtainCurrentSession(RequestContext context) {
    MutableAttributeMap flowScope = context.getFlowScope();
    if (flowScope.get(PERSISTENCE_CONTEXT_ATTRIBUTE) != null) {
        return;/*from  w  w  w. j  av a  2s .c  o m*/
    }

    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
            .getResource(localSessionFactory);
    if (sessionHolder != null) {
        flowScope.put(PERSISTENCE_CONTEXT_ATTRIBUTE, sessionHolder.getSession());
    }
}