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