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:com._4dconcept.springframework.data.marklogic.datasource.ContentSourceUtils.java

/**
 * Actually close the given Session, obtained from the given ContentSource.
 * Same as {@link #releaseSession}, but throwing the original XccException.
 * <p>Directly accessed by {@link TransactionAwareContentSourceProxy}.
 * @param ses the Session to close if necessary
 * (if this is {@code null}, the call will be ignored)
 * @param contentSource the ContentSource that the Session was obtained from
 * (may be {@code null})/* w  w  w .  j av  a 2  s .  co  m*/
 * @throws XccException if thrown by XDBC methods
 * @see #doGetSession
 */
public static void doReleaseSession(Session ses, ContentSource contentSource) throws XccException {
    if (ses == null) {
        return;
    }
    if (contentSource != null) {
        SessionHolder sesHolder = (SessionHolder) TransactionSynchronizationManager.getResource(contentSource);
        if (sesHolder != null && sessionEquals(sesHolder, ses)) {
            // It's the transactional Session: Don't close it.
            sesHolder.released();
            return;
        }
    }
    logger.debug("Returning XDBC Session to ContentSource");
    doCloseSession(ses, contentSource);
}

From source file:org.cfr.capsicum.datasource.DataSourceUtils.java

/**
 * Actually close the given Connection, obtained from the given DataSource.
 * Same as {@link #releaseConnection}, but throwing the original SQLException.
 * <p>Directly accessed by {@link TransactionAwareDataSourceProxy}.
 * @param con the Connection to close if necessary
 * (if this is <code>null</code>, the call will be ignored)
 * @param dataSource the DataSource that the Connection was obtained from
 * (may be <code>null</code>)
 * @throws SQLException if thrown by JDBC methods
 * @see #doGetConnection//from w w  w  . j a  v a  2s .c  o  m
 */
public static void doReleaseConnection(Connection con, DataSource dataSource) throws SQLException {
    if (con == null) {
        return;
    }

    if (dataSource != null) {
        CayenneConnectionHolder conHolder = (CayenneConnectionHolder) TransactionSynchronizationManager
                .getResource(dataSource);
        if (conHolder != null && connectionEquals(conHolder, con)) {
            // It's the transactional Connection: Don't close it.
            conHolder.released();
            return;
        }
    }

    // Leave the Connection open only if the DataSource is our
    // special SmartDataSoruce and it wants the Connection left open.
    if (!(dataSource instanceof SmartDataSource) || ((SmartDataSource) dataSource).shouldClose(con)) {
        logger.debug("Returning JDBC Connection to DataSource");
        con.close();
    }
}

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

/**
 * Adds the session to the current SessionHolder's list of sessions, making it the current session.
 * If there's no current session, calls bindSession.
 * @param session the session/*www.  ja  v a2 s.c  om*/
 * @return the session
 */
public static Session bindNewSession(final Session session) {
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
            .getResource(session.getDatastore());
    if (sessionHolder == null) {
        return bindSession(session);
    }

    sessionHolder.addSession(session);
    return session;
}

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

/**
 * Unbinds and closes a session. If it's the only session in the SessionHolder, unbinds
 * the SessionHolder, otherwise just removes the session from the holder's list.
 * @param session the session/*from  www  . j a  va 2s .  c  o  m*/
 */
public static void unbindSession(final Session session) {
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager
            .getResource(session.getDatastore());
    if (sessionHolder == null) {
        logger.warn("Cannot unbind session, there's no SessionHolder registered");
        return;
    }

    if (!sessionHolder.containsSession(session)) {
        logger.warn("Cannot unbind session, it's not registered in a SessionHolder");
        return;
    }

    if (sessionHolder.size() > 1) {
        sessionHolder.removeSession(session);
    } else {
        TransactionSynchronizationManager.unbindResource(session.getDatastore());
    }

    closeSessionOrRegisterDeferredClose(session, session.getDatastore());
}

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

protected boolean isSessionTransactional(Session session) {
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    return sessionHolder != null && sessionHolder.getSession() == session;
}

From source file:com.turbospaces.spaces.AbstractJSpace.java

@Override
public SpaceTransactionHolder getTransactionHolder() {
    return (SpaceTransactionHolder) TransactionSynchronizationManager.getResource(this);
}

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

/**
 * Prepare the given Query object, applying cache settings and/or a
 * transaction timeout./*from   w  w w.j  a  v  a 2 s . c  om*/
 *
 * @param query the Query object to prepare
 */
protected void prepareQuery(Query query) {
    if (cacheQueries) {
        query.setCacheable(true);
    }
    if (shouldPassReadOnlyToHibernate()) {
        query.setReadOnly(true);
    }
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    if (sessionHolder != null && sessionHolder.hasTimeout()) {
        query.setTimeout(sessionHolder.getTimeToLiveInSeconds());
    }
}

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

/**
 * Prepare the given Criteria object, applying cache settings and/or a
 * transaction timeout.//w  w  w .  ja v  a2s  .c o m
 *
 * @param criteria the Criteria object to prepare
 */
protected void prepareCriteria(Criteria criteria) {
    if (cacheQueries) {
        criteria.setCacheable(true);
    }
    if (shouldPassReadOnlyToHibernate()) {
        criteria.setReadOnly(true);
    }
    SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    if (sessionHolder != null && sessionHolder.hasTimeout()) {
        criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds());
    }
}

From source file:com.ccc.ccm.client.JMSTemplateAutowired.java

/**
* Actually receive a JMS message.//from   w ww. j av  a  2 s . c o  m
* @param session the JMS Session to operate on
* @param consumer the JMS MessageConsumer to receive with
* @return the JMS Message received, or <code>null</code> if none
* @throws JMSException if thrown by JMS API methods
*/
protected Message doReceive(Session session, MessageConsumer consumer) throws JMSException {
    try {
        // Use transaction timeout (if available).
        long timeout = getReceiveTimeout();
        JmsResourceHolder resourceHolder = (JmsResourceHolder) TransactionSynchronizationManager
                .getResource(getConnectionFactory());
        if (resourceHolder != null && resourceHolder.hasTimeout()) {
            timeout = resourceHolder.getTimeToLiveInMillis();
        }
        Message message = doReceive(consumer, timeout);
        if (session.getTransacted()) {
            // Commit necessary - but avoid commit call within a JTA transaction.
            if (isSessionLocallyTransacted(session)) {
                // Transacted session created by this template -> commit.
                JmsUtils.commitIfNecessary(session);
            }
        } else if (isClientAcknowledge(session)) {
            // Manually acknowledge message, if any.
            if (message != null) {
                message.acknowledge();
            }
        }
        return message;
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
    }
}

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

private void cleanUpTransaction(final HttpServletRequest request) {
    if (noTransaction(request)) {
        return;/* www.j a  v a 2 s . c om*/
    }
    logDebug(request, "Cleaning up transaction");

    // Close any open iterators
    DataIteratorHelper.closeOpenIterators();

    // Close the session
    final SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
    if (holder != null) {
        try {
            final Session session = holder.getSession();
            if (session.isOpen()) {
                session.close();
            }
        } catch (final Exception e) {
            LOG.error("Error closing Hibernate session", e);
        }
        TransactionSynchronizationManager.unbindResourceIfPossible(sessionFactory);
    }

    // Close the connection
    final Connection connection = (Connection) TransactionSynchronizationManager
            .getResource(connectionProvider);
    if (connection != null) {
        try {
            connectionProvider.closeConnection(connection);
        } catch (final Exception e) {
            LOG.error("Error closing database connection", e);
        }
        TransactionSynchronizationManager.unbindResourceIfPossible(connectionProvider);
    }

    // Cleanup the Spring transaction data
    TransactionSynchronizationManager.setCurrentTransactionReadOnly(false);
    TransactionSynchronizationManager.setActualTransactionActive(false);

    // Cleanup the current transaction data
    CurrentTransactionData.cleanup();

    request.removeAttribute(EXECUTION_RESULT_KEY);
}