• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Handling commit errors in JPA

 
Eric Jones
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about JPA/JTA error handling. If I commit a transaction, and it fails, what is the proper way to bring my entities back under management, fix the problem, and then re-commit?

Specifically, I am wondering how to determine the exact error that occured (so I can present it to the uer for correction). Also, it is my understanding that all entities become unmanaged after commit failure, so how should I handle that?

Also, are their vendor-specific API's for help with error handling that I should be using?

Thanks for any and all advice. This is a very confusing area to me.

Eric
 
James Sutherland
Ranch Hand
Posts: 553
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPA does not allow re-trying a commit. If any error occurs in the transaction, the transaction is marked for rollback, and all managed objects will become detached.

This makes error handling difficult. Some options include:

- Report the error to the user, and have them retry their work.
- Retry the entire operation, or use SessionBean retry options that some Java EE providers provide.
- Manage your changes in an application manager persistence context (EntityManager), and then merge the changes into a new persistence context to attempt a commit. Handle the error and correct the issue in the application persistence context, and re-merge into a new persistence context to retry.
- Use a vendor specific API, such as EclipseLink's UnitOfWork API that allows for nested units of work, commit retry and resuming on failure.
 
Eric Jones
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the helpful overview. Do you have a recommendation as to which of these is best? Is there a reference where I can read more about these options?

Thanks

Eric
 
Eric Jones
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manage your changes in an application manager persistence context (EntityManager), and then merge the changes into a new persistence context to attempt a commit. Handle the error and correct the issue in the application persistence context, and re-merge into a new persistence context to retry.


Thanks. Do you know where I might find some example code for this?

Eric

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic