users@glassfish.java.net

Re: Catching Database contraint violations within EJBs

From: Mitesh Meswani <Mitesh.Meswani_at_Sun.COM>
Date: Mon, 06 Apr 2009 14:16:18 -0700

glassfish_at_javadesktop.org wrote:
> Thanks for the reply,
>
> I always want to be careful with my use of flush() for synchronizing with the database. Because I think it is an overkill to call flush() after every persist() in all my EAO beans. So, I ask
>
> 1. Do you think that setting the flushMode for the entityManager to FlushModeType.COMMIT will give the same effect and let me catch the exception. This means that at the end of every method marked with TransactionAttributeType.REQUIRED, the flush will take place and I can therefore catch the exception.
>
No. The commit happens after control has returned from your method.
> 2. Though I am yet to try this out, I am thinking that a rollback on a method operation that called flush will not work because the rollback will be working after the synchronization. Example, If I have 5 objects to persist and I am doing it in a for loop. For every insert, I call the EAO bean for an insert operation which makes use of flush(), and the fourth object in the list fails because of constraint violation, though I am supposed to rollback the entire list back, I might I lost the ability to roll everything back because of flush(). How true is this?
>
A failed constrain violation would mark the current transaction for roll
back. Nothing will be inserted into database from the above example.
> Expecting a reply.
> Regards.
> [Message sent by forum member 'okmich' (okmich)]
>
> http://forums.java.net/jive/thread.jspa?messageID=340574
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>