-
1. Re: Transaction annotation - cannot get desired behavior
lpmon Apr 24, 2007 4:45 PM (in response to lpmon)Surely someone has a hint on this. My experience is the transaction annotations have no effect. -OR- I have misinterpreted something.
My experience: no transactions are committed unless the method in the top session bean in the call chain exits.
My understanding:
A method annotated with:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
would commit upon method exit. (not happening)
Shouldn't this start a new transaction and commit upon exit???? -
2. Re: Transaction annotation - cannot get desired behavior
amitka May 20, 2007 10:01 AM (in response to lpmon)One way to solve it, from my experience, would be to inject another instance of that bean to itself, and invoke the method (the one annotated with REQUIRES_NEW) on that injected bean. Otherwise the transaction attributes annotations have no effect because you're not working with the proxy but with the class itself.
i.e.@Stateless public class YourBean impelemts YourBeanLocal { @EJB YourBeanLocal yourBean; public void callingMethod() { ... yourBean.yourRequiresNewMethod(...); ... } @TransactionAttribute(...REQUIRES_NEW) yourRequiresNewMethod() { ... } }
This does have a problem that whenever using this bean you're actually acquiring 2 instances of it.
If you think of a better way to do it, I'll be very glad to know.
HTH. -
3. Re: Transaction annotation - cannot get desired behavior
waynebaylor May 20, 2007 12:03 PM (in response to lpmon)Here's an alternative:
Instead of trying to ignore the exception, try calling em.contains(...) and if that returns false, then execute em.persist(...). -
4. Re: Transaction annotation - cannot get desired behavior
waynebaylor May 20, 2007 12:43 PM (in response to lpmon)Even better, try using em.merge(...) instead of em.persist(...). That way if the entity exists it will be merged and you won't get an exception.
-
5. Re: Transaction annotation - cannot get desired behavior
amitka May 21, 2007 2:58 AM (in response to lpmon)This will only work for EntityExistsException due to primary key constraint. The situation I encountered is with a unique constraint on other columns which are not primary keys.
I used the setter methods to set some field values, and then handled both InvalidStateException to retrieve all of the InvalidValue-s, and EntityExistsException to know if a unique constraint has been violated. The problem is that in the catch block of the EntityExistsException, I couldn't query and extract which field's uniqueness the update tried to violate. The only solution I did find was to receive another instance of the (same) bean (from itself), and invoke a REQUIRES_NEW method on that other instance which does the query.