users@glassfish.java.net

Re: Catching Database contraint violations within EJBs

From: <glassfish_at_javadesktop.org>
Date: Mon, 06 Apr 2009 15:01:45 PDT

You pretty much have to use flush whenever you want to specifically catch a DB exception. When you have a method marked as TranasactionAttributeType.REQUIRED, it will simply join any existing transaction, so the COMMIT is only going to happen when your first method is exited. In any case, the exception if going to happen "within the EJB" glue, and not within your code per se.

Finally, DB exceptions are simply that -- exceptions. You should not be relying on DB integrity checks as "validation" within your code. Specifically with JPA, since ANY exception will obliterate your entire transaction, and you can not recover from it.

Specifically, the classic "do an insert, and if it throws a not-unique-key exception, we'll do an update" idiom simply WILL NOT work with JPA. Check first with a select or select for Update, then update or insert.
[Message sent by forum member 'whartung' (whartung)]

http://forums.java.net/jive/thread.jspa?messageID=340840