Details
Description
Section 7.9.1 of the JPA spec states that in a container managed persistence context (that's what you get with LocalContainerEntityManagerFactoryBean), calls to persist(…), merge(…), etc. need to throw a TransactionRequiredException if there's no transaction in progress. However, we currently don't consider this at all which leads to the effect that code like this will not create an exception but also don't persist the entity:
@Repository public class UserDao { @PersistenceContext private EntityManager em; public void save(User user) { em.persist(user); } }
Additionally calling em.flush() will trigger the exception being thrown by the persistence provider but you might run into this scenario as the code relies on a transaction being started on a layer above. If that's forgotten, you get running (invalid) code, that's not barking at you in any way.
Of course this can be worked around by e.g. using Spring Data JPA (which has defaulting of transactions on the repository level) but I think we should adhere to the spec here.
Attachments
Issue Links
- is related to
-
SPR-14371 Shared EntityManager's target lookup doesn't work with Spring Data's ChainedTransactionManager
-
- Resolved
-
-
SPR-13838 Regression: Shared EntityManager proxy insists on actualTransactiveActive flag even with SYNCHRONIZATION_NEVER
-
- Closed
-
-
SPR-13243 Inconsistent JPA behavior using no transaction, propagation SUPPORTS and OpenEntityManager pattern
-
- Closed
-