• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA exceptions in EJBs: How do you handle them?  RSS feed

 
Avor Nadal
Ranch Hand
Posts: 155
Java Netbeans IDE Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello:

In my EJB modules, to prevent that any JPA exception is ever thrown, I check the condition that would cause the exception beforehand. For example, the exception javax.​persistence.EntityExistsException is never thrown because, before persisting the entity, I check if such primary key already exists in the DB. Is it the right way to do this?

Another approach is too allow the JPA exceptions to be thrown and catch them in a try-catch block, and then throw my custom exception from the "catch" block. However it requires to call EntityManager.flush () at the end of the "try" block. Otherwise the exception throw could be deferred and never be caught by my block.

Thank you.
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exceptions from your JPA layer usually cause the transaction to be rolled back (even if you catch them), so I'd try to prevent them at all times.
 
Avor Nadal
Ranch Hand
Posts: 155
Java Netbeans IDE Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Exceptions from your JPA layer usually cause the transaction to be rolled back (even if you catch them), so I'd try to prevent them at all times.


OK. In that case, I'll continue preventing them. Only one more question: How do you handle the javax.persistence.OptimisticLockException and javax.persistence.PessimisticLockException exceptions? Do you call EntityManager.flush () to be able to catch them in a try-catch block?

Thank you!
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to flush anything to catch them. Just keep in mind (from the Javadoc): "The current transaction, if one is active, will be marked for rollback." So if you catch those and want to retry or something, you have to start a new transaction.
 
Avor Nadal
Ranch Hand
Posts: 155
Java Netbeans IDE Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:You don't need to flush anything to catch them. Just keep in mind (from the Javadoc): "The current transaction, if one is active, will be marked for rollback." So if you catch those and want to retry or something, you have to start a new transaction.


Hello Rob. Thanks again for your prompt answer. In my case it's desirable that the transaction is rolled back, so that behaviour is OK.

Maybe I explained it poorly, sorry. But I wanted to know if such OptimisticLockException can be caught in a try-catch block without the need to flush the changes to the DB, in order to prevent the exception to be thrown, and do something different, such as throwing a custom application exception.

For example, let's suppose that an entity called Student has a field marked with the annotation @Version, so that the JPA provider (Hibernate in this case) can do an optimistic lock automatically. If the transaction is managed by the container, I believe that the mentioned exception could occur at an undetermined time, not in the bean's method necessarily, Right? So this wouldn't work, very probably:



If I'm right, I should do this:



Am I right?

Thank you!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!