Skip to content

Commit 2f8ac91

Browse files
committed
Only attempt to call joinTransaction in case of actual transaction active
Issue: SPR-13242
1 parent a8fb551 commit 2f8ac91

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

spring-orm/src/main/java/org/springframework/orm/jpa/EntityManagerFactoryUtils.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -221,20 +221,23 @@ public static EntityManager doGetTransactionalEntityManager(
221221
(EntityManagerHolder) TransactionSynchronizationManager.getResource(emf);
222222
if (emHolder != null) {
223223
if (synchronizedWithTransaction) {
224-
if (!emHolder.isSynchronizedWithTransaction() &&
225-
TransactionSynchronizationManager.isSynchronizationActive()) {
226-
// Try to explicitly synchronize the EntityManager itself
227-
// with an ongoing JTA transaction, if any.
228-
try {
229-
emHolder.getEntityManager().joinTransaction();
224+
if (!emHolder.isSynchronizedWithTransaction()) {
225+
if (TransactionSynchronizationManager.isActualTransactionActive()) {
226+
// Try to explicitly synchronize the EntityManager itself
227+
// with an ongoing JTA transaction, if any.
228+
try {
229+
emHolder.getEntityManager().joinTransaction();
230+
}
231+
catch (TransactionRequiredException ex) {
232+
logger.debug("Could not join transaction because none was actually active", ex);
233+
}
230234
}
231-
catch (TransactionRequiredException ex) {
232-
logger.debug("Could not join transaction because none was actually active", ex);
235+
if (TransactionSynchronizationManager.isSynchronizationActive()) {
236+
Object transactionData = prepareTransaction(emHolder.getEntityManager(), emf);
237+
TransactionSynchronizationManager.registerSynchronization(
238+
new TransactionalEntityManagerSynchronization(emHolder, emf, transactionData, false));
239+
emHolder.setSynchronizedWithTransaction(true);
233240
}
234-
Object transactionData = prepareTransaction(emHolder.getEntityManager(), emf);
235-
TransactionSynchronizationManager.registerSynchronization(
236-
new TransactionalEntityManagerSynchronization(emHolder, emf, transactionData, false));
237-
emHolder.setSynchronizedWithTransaction(true);
238241
}
239242
// Use holder's reference count to track synchronizedWithTransaction access.
240243
// isOpen() check used below to find out about it.

spring-orm/src/test/java/org/springframework/orm/jpa/JpaTransactionManagerTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -722,7 +722,6 @@ public Object doInTransaction(TransactionStatus status) {
722722
TransactionSynchronizationManager.unbindResource(factory);
723723
}
724724

725-
verify(manager).joinTransaction();
726725
verify(manager).flush();
727726
}
728727

@@ -754,7 +753,6 @@ public Object doInTransaction(TransactionStatus status) {
754753
TransactionSynchronizationManager.unbindResource(factory);
755754
}
756755

757-
verify(manager).joinTransaction();
758756
verify(manager).flush();
759757
verify(manager).clear();
760758
}

0 commit comments

Comments
 (0)