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 transaction.
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: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/core/stateless/StatelessInstanceManagerPoolingTest.java?view=markup&pathrev=686447
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 three).
Feel free to ask for all the help you need. -David