-
1. Re: How to protect an object from concurrent access in EJB3.
elkner Mar 21, 2006 7:26 AM (in response to adver11)@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) ?
-
2. Re: How to protect an object from concurrent access in EJB3.
adver11 Mar 21, 2006 7:56 AM (in response to adver11)elkner, Are you sure?
-
3. Re: How to protect an object from concurrent access in EJB3.
elkner Mar 21, 2006 11:35 AM (in response to adver11)Well, at least if you use a kind of optimistic locking via @Version one of both should fail and the failed transaction should be rolled back (of course only, if your are not chomping the Exceptions, which automatically trigger the rollback ;-)). Just make sure, that you call em.flush() after merge to be able to catch the exception inside the bean method (i.e. actually before the JTA is finished).
If the method does just a simple merge of a simple bean, it might be even relyable without having a NEW TA, since hibernate should catch the problem on flush via @Version. -
4. Re: How to protect an object from concurrent access in EJB3.
adver11 Mar 21, 2006 8:22 PM (in response to adver11)But, catch the Exception not all. I want to solve the clash about protect the Entity Instance object from concurrent access. Like synchronized keyword in regular JavaBean, to limited only one can access the object at the same time.
-
5. Re: How to protect an object from concurrent access in EJB3.
adver11 Mar 21, 2006 8:29 PM (in response to adver11)this complexion also about some resource, like Queue. In PTP mode, we want only one send the message to the Queue at the same time.
-
6. Re: How to protect an object from concurrent access in EJB3.
adver11 Mar 22, 2006 6:32 AM (in response to adver11)@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@TxSynchronized
REQUIRES_NEW?The annotated method is executed within a newly generated transaction.If the caller method is already in a transaction, it is suspended.
@TxSynchronized : When a @TxSynchronized member method is called, the object instance's lock monitor is held for the duration of the transaction, until it commits or rolls back. When a @TxSynchronized annotated static method or constructor is invoked, the Class's lock monitor is held for the duration of the transaction. This lock works like synchronized, in that the lock is only acquired when a tagged member is called.
I think it can solve the clash. -
7. Re: How to protect an object from concurrent access in EJB3.
elkner Mar 22, 2006 7:44 AM (in response to adver11)Not sure, what you are trying to accomplish with it, however, where do you know,
1) that's the EM has only ONE instance of an SLSB?
2) that there is only one EM, which "manages" the SLSB?
3) that not another instance of something accesses your bean from within another ClassLoader?
4) Scaleability is not an issue for you ?
Even if it would work like synchronized, what do you expect/does it buy you? Do you expect, that the method gets always an entity passed, with the latest committed state? If so, I think you have wrong assumptions... -
8. Re: How to protect an object from concurrent access in EJB3.
adver11 Mar 22, 2006 10:52 AM (in response to adver11)So, use @Version? Use Version field each table? but applications that use more than one
Version property or field will not be portable. -
9. Re: How to protect an object from concurrent access in EJB3.
adver11 Mar 22, 2006 10:56 AM (in response to adver11)elkner, Should you give me a example about using @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) ,@TxSynchronized,@Version?
thanks. -
10. Re: How to protect an object from concurrent access in EJB3.
elkner Mar 22, 2006 1:17 PM (in response to adver11)