EntityManager.flush() commits the transaction in a Java web service

时间秒杀一切 提交于 2019-12-05 05:17:14

It seems that there is nothing wrong with flush() after all. The problem was that I didn't set up the data source correctly in JBoss. The lesson here is that if you want to use container managed transactions in EBJs then you need to:

  • In JBoss, check the Use JTA? check box in the data source configuration.
  • In Weblogic, check the Supports Global Transactions check box in the Transactions tab of the data source configuration.

Additionally, and in order to clear any confusion, the transaction management in my code is correct. Throwing a RuntimeException does rollback the Exception. Why is that? Well, from the Java EE 6 tutorial we have:

if a system exception is thrown, the container will automatically roll back the transaction.

But what is a system exception? The tutorial does not seem to touch upon the subject any further, so lets search the EJB spec. In page 382 we have:

A system exception is an exception that is a java.rmi.RemoteException (or one of its sub- classes) or a RuntimeException that is not an application exception.

OK, so maybe the RuntimeException is an application exception then? No it is not, because in page 380 we have this:

Application exceptions that are checked exceptions may be defined as such by being listed in the throws clauses of the methods of the bean’s business interface, no-interface view, home interface, component interface, and web service endpoint. An application exception that is an unchecked exception is defined as an application exception by annotating it with the ApplicationException metadata annotation, or denoting it in the deployment descriptor with the application-exception element.

So, because I didn't do any of the things listed above, the exception I throw in my code is indeed a system exception and indeed rolls back the transaction if you have set up your data source to use JTA.

To rollback a transaction in an EJB method you should call the setRollbackOnly() method, otherwise exiting from a method even throwing an exception causes the transaction to be commited. For a more detailed explanation you can refer to The Java EE 6 Tutorial.

Quoting JSR-317 of the JPA specification page 23:

Runtime exceptions thrown by property accessor methods cause the current transaction to be marked for rollback. Exceptions thrown by such methods when used by the persistence runtime to load or store persistent state cause the persistence runtime to mark the current transaction for rollback and to throw a PersistenceException that wraps the application exception.

Thus the RuntimeException you throw should be thrown from the entity bean setter NOT from the EJB.

I am sure you did not get the wrapping PersistenceException - which indicates the mark for roll back - instead you got the RuntimeException which you threw.

Try throwing RollbackException instead of RuntimeException from the EJB!!!

Or throw the RuntimeException from within the entity bean setters as the specification says.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!