persistence@glassfish.java.net

Re: preventing deadlock

From: Craig L Russell <Craig.Russell_at_Sun.COM>
Date: Fri, 03 Mar 2006 18:54:59 -0800

Hi Scott,

I'm curious. Can you reproduce this on CMP 2.1? I recall that we had
to fix some issues in this area, and it took a bit of time to get it
right.

Craig

On Mar 3, 2006, at 3:46 PM, Scott Oaks wrote:

> Given an entity A and a session bean that executes this code:
>
> Query q = em.findNamedQuery("findAllA");
> List l = q.getResultList();
> Object[] arr = l.toArray();
> Arrays.sort(arr); --> sort by Primary Key of A
> for (int i = 0; i < arr.length; i++)
> ((A)a[i]).setFoo(some value);
>
> When the transaction of the session bean ends, a flurry of update
> statements will be committed to the database. The problem is that
> these
> update statements are not executed in order. Hence if two threads
> simultaneously execute this code, one of them will get a deadlock
> exception, despite the careful sorting of the operations in the
> application.
>
> How is this deadlock to be avoided?
>
> -Scott

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell_at_sun.com
P.S. A good JDO? O, Gasp!