tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Blevins <>
Subject Re: Unit tests for concurrent transactions
Date Tue, 09 Sep 2008 01:33:47 GMT

On Sep 8, 2008, at 4:25 AM, Marcin Kwapisz wrote:

> I would like to make unit tests for concurrent transactions, for  
> example to check optimistic locking.
> Is it possible to perform such tests with embedded OpenEJB? If the  
> answer is: yes, how can I control the order of operations in two  
> concurrent transactions.

It's definitely possible to do.  At minimum you'd need one thread per  

You might want to dig into the java.util.concurrent package to  
guarantee that everything is happening in the exact way you want.   
Basically anytime you find yourself adding Thread.sleep statements,  
you'll be far better off with one or two of the basic "tactics" that  
the java.util.concurrent package offers.

Here's an example of using java.util.concurrent.CountDownLatch which  
might help you:

You could hand your bean a CountDownLatch initialized to 1 and have it  
do some work in a transaction and call "countDownLatch.await()" so the  
method doesn't exit and the transaction stays open.  Then invoke  
another bean that attempts to work with the same data.  When it  
completes, your test code can call countDownLatch.countDown() and the  
bean that is blocking on the await() call will wake up and continue  
with its transaction.

Definitely would make for a cool example/blog post/article (one of the  

Feel free to ask for all the help you need.


View raw message