Enforce TransactionRequiredException for pre-bound EntityManager without actual transaction active
Issue: SPR-13243
This commit is contained in:
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
@@ -271,8 +272,9 @@ public abstract class SharedEntityManagerCreator {
|
|||||||
else if (transactionRequiringMethods.contains(method.getName())) {
|
else if (transactionRequiringMethods.contains(method.getName())) {
|
||||||
// We need a transactional target now, according to the JPA spec.
|
// We need a transactional target now, according to the JPA spec.
|
||||||
// Otherwise, the operation would get accepted but remain unflushed...
|
// Otherwise, the operation would get accepted but remain unflushed...
|
||||||
if (target == null) {
|
if (target == null || !TransactionSynchronizationManager.isActualTransactionActive()) {
|
||||||
throw new TransactionRequiredException("No transactional EntityManager available");
|
throw new TransactionRequiredException("No EntityManager with actual transaction available " +
|
||||||
|
"for current thread - cannot reliably process '" + method.getName() + "' call");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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() {
|
tt.execute(new TransactionCallback() {
|
||||||
@Override
|
@Override
|
||||||
public Object doInTransaction(TransactionStatus status) {
|
public Object doInTransaction(TransactionStatus status) {
|
||||||
bean.sharedEntityManager.flush();
|
bean.sharedEntityManager.clear();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
verify(manager).flush();
|
verify(manager).clear();
|
||||||
verify(manager).close();
|
verify(manager).close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user