Java Code Examples for org.springframework.transaction.support.TransactionSynchronizationManager#unbindResourceIfPossible()

The following examples show how to use org.springframework.transaction.support.TransactionSynchronizationManager#unbindResourceIfPossible() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataSourceUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void afterCompletion(int status) {
	// If we haven't closed the Connection in beforeCompletion,
	// close it now. The holder might have been used for other
	// cleanup in the meantime, for example by a Hibernate Session.
	if (this.holderActive) {
		// The thread-bound ConnectionHolder might not be available anymore,
		// since afterCompletion might get called from a different thread.
		TransactionSynchronizationManager.unbindResourceIfPossible(this.dataSource);
		this.holderActive = false;
		if (this.connectionHolder.hasConnection()) {
			releaseConnection(this.connectionHolder.getConnection(), this.dataSource);
			// Reset the ConnectionHolder: It might remain bound to the thread.
			this.connectionHolder.setConnection(null);
		}
	}
	this.connectionHolder.reset();
}
 
Example 2
Source File: DataSourceUtils.java    From effectivejava with Apache License 2.0 6 votes vote down vote up
@Override
public void afterCompletion(int status) {
	// If we haven't closed the Connection in beforeCompletion,
	// close it now. The holder might have been used for other
	// cleanup in the meantime, for example by a Hibernate Session.
	if (this.holderActive) {
		// The thread-bound ConnectionHolder might not be available anymore,
		// since afterCompletion might get called from a different thread.
		TransactionSynchronizationManager.unbindResourceIfPossible(this.dataSource);
		this.holderActive = false;
		if (this.connectionHolder.hasConnection()) {
			releaseConnection(this.connectionHolder.getConnection(), this.dataSource);
			// Reset the ConnectionHolder: It might remain bound to the thread.
			this.connectionHolder.setConnection(null);
		}
	}
	this.connectionHolder.reset();
}
 
Example 3
Source File: DataSourceUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void afterCompletion(int status) {
	// If we haven't closed the Connection in beforeCompletion,
	// close it now. The holder might have been used for other
	// cleanup in the meantime, for example by a Hibernate Session.
	if (this.holderActive) {
		// The thread-bound ConnectionHolder might not be available anymore,
		// since afterCompletion might get called from a different thread.
		TransactionSynchronizationManager.unbindResourceIfPossible(this.dataSource);
		this.holderActive = false;
		if (this.connectionHolder.hasConnection()) {
			releaseConnection(this.connectionHolder.getConnection(), this.dataSource);
			// Reset the ConnectionHolder: It might remain bound to the thread.
			this.connectionHolder.setConnection(null);
		}
	}
	this.connectionHolder.reset();
}
 
Example 4
Source File: DTSTransactionSynchronizationAdapter.java    From dtsopensource with Apache License 2.0 6 votes vote down vote up
@Override
public void afterCompletion(int status) {
    log.info("本地事务同步状态,activityId:{},status:{}", activityId, status);
    try {

        switch (status) {
            case STATUS_COMMITTED:
                this.transactionCommit();
                break;
            case STATUS_ROLLED_BACK:
                this.transactionRollback();
                break;
            case STATUS_UNKNOWN:
            default:
                this.transactionUnknown();
                break;
        }
    } finally {
        TransactionSynchronizationManager.unbindResourceIfPossible(activityId);
        log.info("--->释放TransactionSynchronizationManager资源...");
        DTSContext.clear();
        log.info("--->释放DTSContext资源...");
    }
}
 
Example 5
Source File: DataSourceUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void afterCompletion(int status) {
	// If we haven't closed the Connection in beforeCompletion,
	// close it now. The holder might have been used for other
	// cleanup in the meantime, for example by a Hibernate Session.
	if (this.holderActive) {
		// The thread-bound ConnectionHolder might not be available anymore,
		// since afterCompletion might get called from a different thread.
		TransactionSynchronizationManager.unbindResourceIfPossible(this.dataSource);
		this.holderActive = false;
		if (this.connectionHolder.hasConnection()) {
			releaseConnection(this.connectionHolder.getConnection(), this.dataSource);
			// Reset the ConnectionHolder: It might remain bound to the thread.
			this.connectionHolder.setConnection(null);
		}
	}
	this.connectionHolder.reset();
}
 
Example 6
Source File: DTSTransactionSynchronizationAdapter.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Override
public void afterCompletion(int status) {
	log.info("本地事务同步状态,activityId:{},status:{}", activityId, status);
	try {

		switch (status) {
		case STATUS_COMMITTED:
			this.transactionCommit();
			break;
		case STATUS_ROLLED_BACK:
			this.transactionRollback();
			break;
		case STATUS_UNKNOWN:
		default:
			this.transactionUnknown();
			break;
		}
	} finally {
		TransactionSynchronizationManager.unbindResourceIfPossible(activityId);
		log.info("--->释放TransactionSynchronizationManager资源...");
		DTSContext.clear();
		log.info("--->释放DTSContext资源...");
	}
}
 
Example 7
Source File: DataSourceUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void afterCompletion(int status) {
	// If we haven't closed the Connection in beforeCompletion,
	// close it now. The holder might have been used for other
	// cleanup in the meantime, for example by a Hibernate Session.
	if (this.holderActive) {
		// The thread-bound ConnectionHolder might not be available anymore,
		// since afterCompletion might get called from a different thread.
		TransactionSynchronizationManager.unbindResourceIfPossible(this.dataSource);
		this.holderActive = false;
		if (this.connectionHolder.hasConnection()) {
			releaseConnection(this.connectionHolder.getConnection(), this.dataSource);
			// Reset the ConnectionHolder: It might remain bound to the thread.
			this.connectionHolder.setConnection(null);
		}
	}
	this.connectionHolder.reset();
}
 
Example 8
Source File: PostgreSqlTransactionManager.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void doCleanupAfterCompletion(Object transaction) {
  MolgenisTransaction molgenisTransaction = (MolgenisTransaction) transaction;
  if (LOG.isDebugEnabled()) {
    LOG.debug("Cleanup transaction [{}]", molgenisTransaction.getId());
  }

  super.doCleanupAfterCompletion(molgenisTransaction.getDataSourceTransaction());
  TransactionSynchronizationManager.unbindResourceIfPossible(
      TransactionConstants.TRANSACTION_ID_RESOURCE_NAME);

  transactionListeners.forEach(j -> j.doCleanupAfterCompletion(molgenisTransaction.getId()));
}
 
Example 9
Source File: JpaTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void doCleanupAfterCompletion(Object transaction) {
	JpaTransactionObject txObject = (JpaTransactionObject) transaction;

	// Remove the entity manager holder from the thread, if still there.
	// (Could have been removed by EntityManagerFactoryUtils in order
	// to replace it with an unsynchronized EntityManager).
	if (txObject.isNewEntityManagerHolder()) {
		TransactionSynchronizationManager.unbindResourceIfPossible(getEntityManagerFactory());
	}
	txObject.getEntityManagerHolder().clear();

	// Remove the JDBC connection holder from the thread, if exposed.
	if (txObject.hasConnectionHolder()) {
		TransactionSynchronizationManager.unbindResource(getDataSource());
		try {
			getJpaDialect().releaseJdbcConnection(txObject.getConnectionHolder().getConnectionHandle(),
					txObject.getEntityManagerHolder().getEntityManager());
		}
		catch (Exception ex) {
			// Just log it, to keep a transaction-related exception.
			logger.error("Could not close JDBC connection after transaction", ex);
		}
	}

	getJpaDialect().cleanupTransaction(txObject.getTransactionData());

	// Remove the entity manager holder from the thread.
	if (txObject.isNewEntityManagerHolder()) {
		EntityManager em = txObject.getEntityManagerHolder().getEntityManager();
		if (logger.isDebugEnabled()) {
			logger.debug("Closing JPA EntityManager [" + em + "] after transaction");
		}
		EntityManagerFactoryUtils.closeEntityManager(em);
	}
	else {
		logger.debug("Not closing pre-bound JPA EntityManager after transaction");
	}
}
 
Example 10
Source File: JpaTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void doCleanupAfterCompletion(Object transaction) {
	JpaTransactionObject txObject = (JpaTransactionObject) transaction;

	// Remove the entity manager holder from the thread, if still there.
	// (Could have been removed by EntityManagerFactoryUtils in order
	// to replace it with an unsynchronized EntityManager).
	if (txObject.isNewEntityManagerHolder()) {
		TransactionSynchronizationManager.unbindResourceIfPossible(getEntityManagerFactory());
	}
	txObject.getEntityManagerHolder().clear();

	// Remove the JDBC connection holder from the thread, if exposed.
	if (txObject.hasConnectionHolder()) {
		TransactionSynchronizationManager.unbindResource(getDataSource());
		try {
			getJpaDialect().releaseJdbcConnection(txObject.getConnectionHolder().getConnectionHandle(),
					txObject.getEntityManagerHolder().getEntityManager());
		}
		catch (Exception ex) {
			// Just log it, to keep a transaction-related exception.
			logger.error("Could not close JDBC connection after transaction", ex);
		}
	}

	getJpaDialect().cleanupTransaction(txObject.getTransactionData());

	// Remove the entity manager holder from the thread.
	if (txObject.isNewEntityManagerHolder()) {
		EntityManager em = txObject.getEntityManagerHolder().getEntityManager();
		if (logger.isDebugEnabled()) {
			logger.debug("Closing JPA EntityManager [" + em + "] after transaction");
		}
		EntityManagerFactoryUtils.closeEntityManager(em);
	}
	else {
		logger.debug("Not closing pre-bound JPA EntityManager after transaction");
	}
}
 
Example 11
Source File: DataSourceTransactionManagerHandlerTest.java    From opensharding-spi-impl with Apache License 2.0 5 votes vote down vote up
@Test(expected = ShardingException.class)
public void assertSwitchTransactionTypeFailGetConnection() throws SQLException {
    when(dataSource.getConnection()).thenThrow(new SQLException("Mock get connection failed"));
    try {
        dataSourceTransactionManagerHandler.switchTransactionType(TransactionType.XA);
    } finally {
        TransactionSynchronizationManager.unbindResourceIfPossible(dataSource);
    }
}
 
Example 12
Source File: DataSourceTransactionManagerHandlerTest.java    From opensharding-spi-impl with Apache License 2.0 5 votes vote down vote up
@Test(expected = ShardingException.class)
public void assertSwitchTransactionTypeFailExecute() throws SQLException {
    Connection connection = mock(Connection.class);
    Statement statement = mock(Statement.class);
    when(dataSource.getConnection()).thenReturn(connection);
    when(connection.createStatement()).thenReturn(statement);
    when(statement.execute(anyString())).thenThrow(new SQLException("Mock send switch transaction type SQL failed"));
    try {
        dataSourceTransactionManagerHandler.switchTransactionType(TransactionType.XA);
    } finally {
        TransactionSynchronizationManager.unbindResourceIfPossible(dataSource);
    }
}
 
Example 13
Source File: DataSourceTransactionManagerHandlerTest.java    From opensharding-spi-impl with Apache License 2.0 5 votes vote down vote up
@Test
public void assertSwitchTransactionTypeSuccess() throws SQLException {
    Connection connection = mock(Connection.class);
    Statement statement = mock(Statement.class);
    when(dataSource.getConnection()).thenReturn(connection);
    when(connection.createStatement()).thenReturn(statement);
    dataSourceTransactionManagerHandler.switchTransactionType(TransactionType.XA);
    verify(statement).execute(anyString());
    TransactionSynchronizationManager.unbindResourceIfPossible(dataSource);
}
 
Example 14
Source File: JpaTransactionManagerHandlerTest.java    From opensharding-spi-impl with Apache License 2.0 5 votes vote down vote up
@Test(expected = ShardingException.class)
public void assertSwitchTransactionTypeFailExecute() throws SQLException {
    when(statement.execute(anyString())).thenThrow(new SQLException("Mock send switch transaction type SQL failed"));
    try {
        jpaTransactionManagerHandler.switchTransactionType(TransactionType.XA);
    } finally {
        TransactionSynchronizationManager.unbindResourceIfPossible(entityManagerFactory);
    }
}
 
Example 15
Source File: JpaTransactionManagerHandlerTest.java    From opensharding-spi-impl with Apache License 2.0 4 votes vote down vote up
@Test
public void assertSwitchTransactionTypeSuccess() throws SQLException {
    jpaTransactionManagerHandler.switchTransactionType(TransactionType.XA);
    verify(statement).execute(anyString());
    TransactionSynchronizationManager.unbindResourceIfPossible(entityManagerFactory);
}
 
Example 16
Source File: JpaTransactionManagerHandler.java    From opensharding-spi-impl with Apache License 2.0 4 votes vote down vote up
@Override
public void unbindResource() {
    EntityManagerHolder entityManagerHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResourceIfPossible(transactionManager.getEntityManagerFactory());
    EntityManagerFactoryUtils.closeEntityManager(entityManagerHolder.getEntityManager());
}
 
Example 17
Source File: RedissonTransactionManager.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
protected void doCleanupAfterCompletion(Object transaction) {
    TransactionSynchronizationManager.unbindResourceIfPossible(redisson);
    RedissonTransactionObject to = (RedissonTransactionObject) transaction;
    to.getTransactionHolder().setTransaction(null);
}
 
Example 18
Source File: JpaTransactionManager.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected void doCleanupAfterCompletion(Object transaction) {
	JpaTransactionObject txObject = (JpaTransactionObject) transaction;

	// Remove the entity manager holder from the thread, if still there.
	// (Could have been removed by EntityManagerFactoryUtils in order
	// to replace it with an unsynchronized EntityManager).
	if (txObject.isNewEntityManagerHolder()) {
		TransactionSynchronizationManager.unbindResourceIfPossible(obtainEntityManagerFactory());
	}
	txObject.getEntityManagerHolder().clear();

	// Remove the JDBC connection holder from the thread, if exposed.
	if (getDataSource() != null && txObject.hasConnectionHolder()) {
		TransactionSynchronizationManager.unbindResource(getDataSource());
		ConnectionHandle conHandle = txObject.getConnectionHolder().getConnectionHandle();
		if (conHandle != null) {
			try {
				getJpaDialect().releaseJdbcConnection(conHandle,
						txObject.getEntityManagerHolder().getEntityManager());
			}
			catch (Exception ex) {
				// Just log it, to keep a transaction-related exception.
				logger.error("Could not close JDBC connection after transaction", ex);
			}
		}
	}

	getJpaDialect().cleanupTransaction(txObject.getTransactionData());

	// Remove the entity manager holder from the thread.
	if (txObject.isNewEntityManagerHolder()) {
		EntityManager em = txObject.getEntityManagerHolder().getEntityManager();
		if (logger.isDebugEnabled()) {
			logger.debug("Closing JPA EntityManager [" + em + "] after transaction");
		}
		EntityManagerFactoryUtils.closeEntityManager(em);
	}
	else {
		logger.debug("Not closing pre-bound JPA EntityManager after transaction");
	}
}
 
Example 19
Source File: JpaTransactionManager.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected void doCleanupAfterCompletion(Object transaction) {
	JpaTransactionObject txObject = (JpaTransactionObject) transaction;

	// Remove the entity manager holder from the thread, if still there.
	// (Could have been removed by EntityManagerFactoryUtils in order
	// to replace it with an unsynchronized EntityManager).
	if (txObject.isNewEntityManagerHolder()) {
		TransactionSynchronizationManager.unbindResourceIfPossible(obtainEntityManagerFactory());
	}
	txObject.getEntityManagerHolder().clear();

	// Remove the JDBC connection holder from the thread, if exposed.
	if (getDataSource() != null && txObject.hasConnectionHolder()) {
		TransactionSynchronizationManager.unbindResource(getDataSource());
		ConnectionHandle conHandle = txObject.getConnectionHolder().getConnectionHandle();
		if (conHandle != null) {
			try {
				getJpaDialect().releaseJdbcConnection(conHandle,
						txObject.getEntityManagerHolder().getEntityManager());
			}
			catch (Exception ex) {
				// Just log it, to keep a transaction-related exception.
				logger.error("Could not close JDBC connection after transaction", ex);
			}
		}
	}

	getJpaDialect().cleanupTransaction(txObject.getTransactionData());

	// Remove the entity manager holder from the thread.
	if (txObject.isNewEntityManagerHolder()) {
		EntityManager em = txObject.getEntityManagerHolder().getEntityManager();
		if (logger.isDebugEnabled()) {
			logger.debug("Closing JPA EntityManager [" + em + "] after transaction");
		}
		EntityManagerFactoryUtils.closeEntityManager(em);
	}
	else {
		logger.debug("Not closing pre-bound JPA EntityManager after transaction");
	}
}