提交 792991a8 编写于 作者: J Juergen Hoeller

Cleanup after unexpected exception from external delegation call

Issue: SPR-17559

(cherry picked from commit c024bdcc)
上级 b8480ea0
...@@ -111,21 +111,28 @@ public abstract class DataSourceUtils { ...@@ -111,21 +111,28 @@ public abstract class DataSourceUtils {
Connection con = dataSource.getConnection(); Connection con = dataSource.getConnection();
if (TransactionSynchronizationManager.isSynchronizationActive()) { if (TransactionSynchronizationManager.isSynchronizationActive()) {
// Use same Connection for further JDBC actions within the transaction. try {
// Thread-bound object will get removed by synchronization at transaction completion. // Use same Connection for further JDBC actions within the transaction.
ConnectionHolder holderToUse = conHolder; // Thread-bound object will get removed by synchronization at transaction completion.
if (holderToUse == null) { ConnectionHolder holderToUse = conHolder;
holderToUse = new ConnectionHolder(con); if (holderToUse == null) {
} holderToUse = new ConnectionHolder(con);
else { }
holderToUse.setConnection(con); else {
holderToUse.setConnection(con);
}
holderToUse.requested();
TransactionSynchronizationManager.registerSynchronization(
new ConnectionSynchronization(holderToUse, dataSource));
holderToUse.setSynchronizedWithTransaction(true);
if (holderToUse != conHolder) {
TransactionSynchronizationManager.bindResource(dataSource, holderToUse);
}
} }
holderToUse.requested(); catch (RuntimeException ex) {
TransactionSynchronizationManager.registerSynchronization( // Unexpected exception from external delegation call -> close Connection and rethrow.
new ConnectionSynchronization(holderToUse, dataSource)); releaseConnection(con, dataSource);
holderToUse.setSynchronizedWithTransaction(true); throw ex;
if (holderToUse != conHolder) {
TransactionSynchronizationManager.bindResource(dataSource, holderToUse);
} }
} }
......
...@@ -285,21 +285,28 @@ public abstract class EntityManagerFactoryUtils { ...@@ -285,21 +285,28 @@ public abstract class EntityManagerFactoryUtils {
em = (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager()); em = (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager());
} }
// Use same EntityManager for further JPA operations within the transaction. try {
// Thread-bound object will get removed by synchronization at transaction completion. // Use same EntityManager for further JPA operations within the transaction.
emHolder = new EntityManagerHolder(em); // Thread-bound object will get removed by synchronization at transaction completion.
if (synchronizedWithTransaction) { emHolder = new EntityManagerHolder(em);
Object transactionData = prepareTransaction(em, emf); if (synchronizedWithTransaction) {
TransactionSynchronizationManager.registerSynchronization( Object transactionData = prepareTransaction(em, emf);
new TransactionalEntityManagerSynchronization(emHolder, emf, transactionData, true)); TransactionSynchronizationManager.registerSynchronization(
emHolder.setSynchronizedWithTransaction(true); new TransactionalEntityManagerSynchronization(emHolder, emf, transactionData, true));
emHolder.setSynchronizedWithTransaction(true);
}
else {
// Unsynchronized - just scope it for the transaction, as demanded by the JPA 2.1 spec...
TransactionSynchronizationManager.registerSynchronization(
new TransactionScopedEntityManagerSynchronization(emHolder, emf));
}
TransactionSynchronizationManager.bindResource(emf, emHolder);
} }
else { catch (RuntimeException ex) {
// Unsynchronized - just scope it for the transaction, as demanded by the JPA 2.1 spec... // Unexpected exception from external delegation call -> close EntityManager and rethrow.
TransactionSynchronizationManager.registerSynchronization( closeEntityManager(em);
new TransactionScopedEntityManagerSynchronization(emHolder, emf)); throw ex;
} }
TransactionSynchronizationManager.bindResource(emf, emHolder);
return em; return em;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册