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:com.healthcit.cacure.businessdelegates.export.DataExportTest.java

@After
public void tearDown() throws Exception {
    TransactionSynchronizationManager.unbindResourceIfPossible(emf);
}

From source file:com.healthcit.cacure.model.GetAllQuestionLibraryQuestions.java

@After
public void tearDown() throws Exception {
    TransactionSynchronizationManager.unbindResourceIfPossible(emf);
    //           TransactionSynchronizationManager.unbindResource(this.sessionFactory);
    //           SessionFactoryUtils.releaseSession(this.session, this.sessionFactory);
}

From source file:com.healthcit.cacure.dao.QuestionDaoTest.java

@After
public void tearDown() throws Exception {
    TransactionSynchronizationManager.unbindResourceIfPossible(emf);
    //        TransactionSynchronizationManager.unbindResource(this.sessionFactory);
    //        SessionFactoryUtils.releaseSession(this.session, this.sessionFactory);
}

From source file:com.rainy.redis.connection.RedisConnectionUtils.java

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

From source file:org.motechproject.server.omod.sdsched.TxSyncManWrapperImplTest.java

public void testGetResource() {
    final String resourceName = "A Resource";
    final String resourceValue = "A Resource value";
    Object retVal = txTempl.execute(new TransactionCallback() {
        public Object doInTransaction(TransactionStatus status) {
            try {
                TransactionSynchronizationManager.bindResource(resourceName, resourceValue);
                return txSyncManWrapper.getResource(resourceName);
            } finally {
                TransactionSynchronizationManager.unbindResourceIfPossible(resourceName);
            }// w  w  w  . ja  va 2  s  .  c  om
        }
    });
    assertEquals(resourceValue, retVal);
}

From source file:org.motechproject.server.omod.sdsched.TxSyncManWrapperImplTest.java

public void testBindResource() {
    final String resourceName = "A Resource";
    final String resourceValue = "A Resource value";
    Object retVal = txTempl.execute(new TransactionCallback() {
        public Object doInTransaction(TransactionStatus status) {
            try {
                txSyncManWrapper.bindResource(resourceName, resourceValue);
                return TransactionSynchronizationManager.getResource(resourceName);
            } finally {
                TransactionSynchronizationManager.unbindResourceIfPossible(resourceName);
            }//  w w  w  .j a  va2  s. c  o  m
        }
    });
    assertEquals(resourceValue, retVal);
}

From source file:org.motechproject.server.omod.sdsched.TxSyncManWrapperImplTest.java

public void testUnbindResource() {
    final String resourceName = "A Resource";
    final String resourceValue = "A Resource value";
    Object retVal = txTempl.execute(new TransactionCallback() {
        public Object doInTransaction(TransactionStatus status) {
            try {
                TransactionSynchronizationManager.bindResource(resourceName, resourceValue);
                txSyncManWrapper.unbindResource(resourceName);
                return txSyncManWrapper.getResource(resourceName);
            } finally {
                TransactionSynchronizationManager.unbindResourceIfPossible(resourceName);
            }//from   ww  w.j a  v a  2  s . c o m
        }
    });
    assertNull(resourceValue, retVal);
}

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

private void cleanUpTransaction(final HttpServletRequest request) {
    if (noTransaction(request)) {
        return;/*from w  w  w  .  ja  v a2 s.com*/
    }
    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);
}

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

private void commitOrRollbackTransaction(final HttpServletRequest request)
        throws IOException, ServletException {
    if (noTransaction(request)) {
        return;//from   ww  w  .j a  v a2 s  .  c  o m
    }
    final ExecutionResult result = (ExecutionResult) request.getAttribute(EXECUTION_RESULT_KEY);
    final SessionHolder sessionHolder = getSessionHolder();
    // Commit or rollback the transaction
    boolean runCommitListeners = false;
    boolean lockingException = false;
    if (result.commit) {
        logDebug(request, "Committing transaction");
        runCommitListeners = true; // Marked as commit - should run commit listeners
        try {
            sessionHolder.getTransaction().commit();
        } catch (final Throwable t) {
            // In case of locking exceptions, we must make sure the correct exception type is returned, so the transaction will be retried
            lockingException = ExceptionHelper.isLockingException(t);
            result.error = t;
        }
    } else {
        if (result.error == null && !result.hasWrite) {
            // Transaction was semantically a commit, so, the commit listeners should run.
            // However, as there where no writes the transaction will be rolled back
            runCommitListeners = true;
            logDebug(request, "Nothing written to database. Rolling-back transaction");
        } else {
            logDebug(request, "Rolling-back transaction");
        }
        sessionHolder.getTransaction().rollback();
    }

    // Disconnect the session
    sessionHolder.getSession().disconnect();

    if (lockingException) {
        // There was a locking exception - throw it now, so the transaction will be retried
        cleanUpTransaction(request);
        throw new LockingException();
    }

    // Unbind the session holder, so that listeners which should open a new transaction on this same thread won't be messed up
    TransactionSynchronizationManager.unbindResourceIfPossible(sessionFactory);

    // Run the transaction listener
    CurrentTransactionData.detachListeners().runListeners(runCommitListeners);

    // Bind the session holder again
    TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);

    // Log the execution if a regular user is logged in and this is not an AJAX request
    if (result.traceLog) {
        traceLog(request, result.error, result.commit);
    }

    // The resulting error was not silenced (i.e, by the BaseAction's try / catch. Log and rethrow
    if (result.error != null && !result.errorWasSilenced) {
        actionHelper.generateLog(request, servlet.getServletContext(), result.error);
        ActionHelper.throwException(result.error);
    }
}