users@glassfish.java.net

Optimistic Locking and deadlocks, why?

From: <glassfish_at_javadesktop.org>
Date: Wed, 26 Nov 2008 04:47:03 PST

Hi!
I'm using glassfish v2 on linux with [b]Optimistic Locked entities[/b] (correctly applied @version).
Now i'm getting dead locks on the production server (the load increased recently) which lock up the whole server.

I want to force the updated data of a detached entity into the db.

0. For all items in Catalog
1. I reload the item from the database
2. Overwrite the loaded item with the data form the updated(dtached) entity (all fields except of the version field)
3. Merge
4. Flush to to make sure data is in the database

The here produced SQL Update produce the [b]deadlock[/b].
I think there should not be any locking because I'm using optimistic approach.

The code runs periodically(timer triggered) and might be executed in parallel if the last execution didn't finish in time.

Thanks
  -stephan


for (final CatalogItem catalogItem : catalog.getItems().values())
                {


final CatalogItem reloadedItem = em.find(CatalogItem.class, catalogItem.getId());
                                if (reloadedItem != null)
                                {
                                        try
                                        {
                                                //item already exist force overwrite <-- i hope this works
                                                reloadedItem.forceOverwrite(catalogItem);
                                                em.merge(reloadedItem);
                                                em.flush();
                                        } catch (final OptimisticLockException e)
                                        {
                                                LOG.severe(String.format("OptimisticLockException for %s, will lose changes.", catalogItem.getId()));
                                        }

                                }

}
[Message sent by forum member 'iceandfire' (iceandfire)]

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