users@glassfish.java.net

Re: Catching Database contraint violations within EJBs

From: Mitesh Meswani <Mitesh.Meswani_at_Sun.COM>
Date: Fri, 03 Apr 2009 14:49:14 -0700

The actual flush to database happens when transaction commits which
happens after your method returns. You can manually flush after your
persist to force a flush and hit the constraint violation like as follows

try{
       entityManager.persist(o);
       entityManager.flush();
} catch (PersistenceException e) {
       handle the exception here
}



glassfish_at_javadesktop.org wrote:
> Good day all,
>
> I have been having an issue for a long time now.
> When I try to insert an entity with the entityManger.persiste(), and there is an underlying constraint violation in the database table. I get an exception that I am always not able to catch. I mean for example
>
> try{
> entityManager.persist(o);
> }catch (Exception ex){
> ex.printStackTrace();
> }
>
> I don't know why control doesn't enter my catch block. Instead, a TransactionRolledBackException or EJBException is thrown and client application often don't deal with this. I found out that the Exceptions thrown are RuntimeException, but RuntimeException is a subclass of Exception, so I assume it should have enter the catch block.
>
> Question
> 1. What could be wrong with this style of coding?
> 2. It is advisable to catch a Throwable within my EJBs?
>
> What else could be done.
> [Message sent by forum member 'okmich' (okmich)]
>
> http://forums.java.net/jive/thread.jspa?messageID=340440
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>