From 7e2a662f637a1b2cecb2d99a6c1d119cd1b4acbf Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 17 Jul 2015 15:25:29 +0200 Subject: [PATCH] Enforce TransactionRequiredException for pre-bound EntityManager without actual transaction active Issue: SPR-13243 --- .../orm/jpa/SharedEntityManagerCreator.java | 8 +++++--- .../jpa/support/PersistenceContextTransactionTests.java | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java b/spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java index e7d9398b3c..3bf9e11a35 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ import javax.persistence.TransactionRequiredException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; @@ -271,8 +272,9 @@ public abstract class SharedEntityManagerCreator { else if (transactionRequiringMethods.contains(method.getName())) { // We need a transactional target now, according to the JPA spec. // Otherwise, the operation would get accepted but remain unflushed... - if (target == null) { - throw new TransactionRequiredException("No transactional EntityManager available"); + if (target == null || !TransactionSynchronizationManager.isActualTransactionActive()) { + throw new TransactionRequiredException("No EntityManager with actual transaction available " + + "for current thread - cannot reliably process '" + method.getName() + "' call"); } } diff --git a/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceContextTransactionTests.java b/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceContextTransactionTests.java index a04c5ce3bc..412e11fd93 100644 --- a/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceContextTransactionTests.java +++ b/spring-orm/src/test/java/org/springframework/orm/jpa/support/PersistenceContextTransactionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -116,12 +116,12 @@ public class PersistenceContextTransactionTests { tt.execute(new TransactionCallback() { @Override public Object doInTransaction(TransactionStatus status) { - bean.sharedEntityManager.flush(); + bean.sharedEntityManager.clear(); return null; } }); - verify(manager).flush(); + verify(manager).clear(); verify(manager).close(); }