[jboss-user] [JBoss Seam] - Handling database exceptions

shakenbrain do-not-reply at jboss.com
Tue Jun 26 16:34:28 EDT 2007


When persisting an entity at the end of a conversation, how can one handle a database exception, like a constraint violation, without invalidating the session?  Here's the the basic scenario:

1.  Start a conversation (flushMode=MANUAL); display edit page
2.  Call a method to persist the edited object; calls entityManager.flush()
3.  A unique constraint is violated; EntityExistsException is thrown
4.  Exeption is caught and a FacesMessage created
5.  Control returns to the edit page
6.  Edit page accesses database to fetch entities for a <s:selectItems> tag
7.  Exception thrown because transaction no longer valid

Gavin states somewhere on this forum that it's a good idea to check the database before persisting an object to see if a constraint would be violated.  I'd prefer to keep my constraint logic in the database- it means a lot less coding in action methods. For example, I don't have to worry about whether the object getting flushed is new or not.  Also, verifying a constraint won't be violated before persisting does not guarantee that the constraint won't be violated in the database (ie, another session inserts an identical object at the same time).  Validating constraints in the control layer just isn't robust enough.

So, is there a way to keep the current session intact when a database exception is thrown?  Section 5.3.1 in the reference manual seems to indicate that the answer is 'no'.  If that's true, that seems to be a pretty major shortcoming (of EJB3, I think).

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4057884#4057884

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4057884



More information about the jboss-user mailing list