users@glassfish.java.net

Strange behavior with UserTransaction

From: <glassfish_at_javadesktop.org>
Date: Mon, 07 Jan 2008 06:47:35 PST

We doing transaction demarcation on the client-side using UserTransaction. UserTransaction is a thread-safe object, and as long as the same object is shared among all threads, everything is fine. But if each thread do a lookup and obtain its own reference, then I get very strange result: when several UserTransaction object are used concurrently (one UserTransaction per thread), then the transaction seems not to be ACID at all !

To demonstrate this problem, I wrote a small test case. There is one EJB that contains one method that will insert [i]N[/i] entries in a table and another method that can be used to count the number of entries. The bean is annotated with @REQUIRED. My application creates several thread that will insert 10 entries in one transaction. There is also a background thread that periodically counts the number of entries in the table.

If transaction isolation is ok, my background thread will count a number of entries that is always a multiple of 10. This is the case when I use a unique UserTransaction object for all threads. But when each thread uses its own UserTransaction, then the background thread count number of entries such a 6, 8 or 12 - showing that transaction is not ACID any more.

The problem can be easily solved by using a single UserTransaction for all thread, but I am very suprised of this behavior. I never read anywhere that the UserTransaction should only be looked once per JVM ! Is this the intended behavior?
[Message sent by forum member 'ewernli' (ewernli)]

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