What is the correct way of rolling back the transaction of a container managed JTA transaction?
I understand this: EJBException when calling entityManager.getTransaction() that I can't get the transaction instance. I read a solution here but I'm not sure whether this is correct.
I'm also aware that, if I throw an exception, the transaction will be rolled back.
But my question is: if I want to (must) use a container managed EntityManager
, what's the correct way of rolling back a transaction inside it?
JPA (Java Persistence API) is the Java ORM standard/specification for storing, accessing, and managing Java objects in a relational database. Hibernate is an implementation of the Java Persistence API (JPA) specification. JTA (Java Transaction API) is the Java standard/specification for distributed transactions.
Spring Boot supports distributed JTA transactions across multiple XA resources by using either an Atomikos or Bitronix embedded transaction manager. JTA transactions are also supported when deploying to a suitable Java EE Application Server.
The asnwer on Code Ranch is right, you have to use SessionContext
@Resource
private SessionContext ctx;
//and then in method
if(fail) {
ctx.setRollBackOnly();
}
See also this article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With