Skip to content

Commit 7e2a662

Browse files
committed
Enforce TransactionRequiredException for pre-bound EntityManager without actual transaction active
Issue: SPR-13243
1 parent 2f8ac91 commit 7e2a662

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

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

Lines changed: 5 additions & 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.
@@ -34,6 +34,7 @@
3434
import org.apache.commons.logging.Log;
3535
import org.apache.commons.logging.LogFactory;
3636

37+
import org.springframework.transaction.support.TransactionSynchronizationManager;
3738
import org.springframework.util.ClassUtils;
3839
import org.springframework.util.CollectionUtils;
3940

@@ -271,8 +272,9 @@ else if (method.getName().equals("unwrap")) {
271272
else if (transactionRequiringMethods.contains(method.getName())) {
272273
// We need a transactional target now, according to the JPA spec.
273274
// Otherwise, the operation would get accepted but remain unflushed...
274-
if (target == null) {
275-
throw new TransactionRequiredException("No transactional EntityManager available");
275+
if (target == null || !TransactionSynchronizationManager.isActualTransactionActive()) {
276+
throw new TransactionRequiredException("No EntityManager with actual transaction available " +
277+
"for current thread - cannot reliably process '" + method.getName() + "' call");
276278
}
277279
}
278280

spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceContextTransactionTests.java

Lines changed: 3 additions & 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.
@@ -116,12 +116,12 @@ public void testTransactionCommitWithSharedEntityManagerAndPropagationSupports()
116116
tt.execute(new TransactionCallback() {
117117
@Override
118118
public Object doInTransaction(TransactionStatus status) {
119-
bean.sharedEntityManager.flush();
119+
bean.sharedEntityManager.clear();
120120
return null;
121121
}
122122
});
123123

124-
verify(manager).flush();
124+
verify(manager).clear();
125125
verify(manager).close();
126126
}
127127

0 commit comments

Comments
 (0)