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

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

Introduction

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

Prototype

@Nullable
public static Object unbindResourceIfPossible(Object key) 

Source Link

Document

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

Usage

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

private void openReadWriteConnection(final HttpServletRequest request) throws IOException, ServletException {
    if (noTransaction(request)) {
        return;/*www.ja  v a 2  s. com*/
    }
    logDebug(request, "Opening a new read-write transaction");
    // Open a read-write transaction
    Connection connection = null;
    Session session = null;
    SessionHolder holder = null;
    Transaction transaction = null;
    try {
        connection = connectionProvider.getConnection();
        TransactionSynchronizationManager.bindResource(connectionProvider, connection);
        session = sessionFactory.openSession(connection);
        holder = new SessionHolder(session);
        transaction = session.beginTransaction();
        holder.setTransaction(transaction);
        TransactionSynchronizationManager.bindResource(sessionFactory, holder);
        holder.setSynchronizedWithTransaction(true);
        TransactionSynchronizationManager.setActualTransactionActive(true);
        TransactionSynchronizationManager.setCurrentTransactionReadOnly(false);
    } catch (final Exception e) {
        if (connection != null) {
            try {
                connectionProvider.closeConnection(connection);
            } catch (final SQLException e1) {
                LOG.warn("Error closing connection", e1);
            } finally {
                TransactionSynchronizationManager.unbindResourceIfPossible(connectionProvider);
                TransactionSynchronizationManager.unbindResourceIfPossible(sessionFactory);
            }
        }
        LOG.error("Couldn't open a transaction", e);
        ActionHelper.throwException(e);
    }
}

From source file:org.springframework.data.keyvalue.redis.core.RedisConnectionUtils.java

/**
 * Unbinds and closes the connection (if any) associated with the given factory.
 * //  ww  w  .ja v a  2s  . c  o m
 * @param factory Redis factory
 */
public static void unbindConnection(RedisConnectionFactory factory) {
    RedisConnectionHolder connHolder = (RedisConnectionHolder) TransactionSynchronizationManager
            .unbindResourceIfPossible(factory);
    if (connHolder != null) {
        RedisConnection connection = connHolder.getConnection();
        connection.close();
    }
}

From source file:org.springframework.data.redis.core.RedisConnectionUtils.java

/**
 * Unbinds and closes the connection (if any) associated with the given factory.
 * /*from ww w  .ja v  a2 s.c om*/
 * @param factory Redis factory
 */
public static void unbindConnection(RedisConnectionFactory factory) {

    RedisConnectionHolder connHolder = (RedisConnectionHolder) TransactionSynchronizationManager
            .unbindResourceIfPossible(factory);

    if (connHolder != null) {
        if (connHolder.isTransactionSyncronisationActive()) {
            if (log.isDebugEnabled()) {
                log.debug("Redis Connection will be closed when outer transaction finished.");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Closing bound connection.");
            }
            RedisConnection connection = connHolder.getConnection();
            connection.close();
        }
    }

}