-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: optimistic locking issues
PostPosted: Wed Sep 28, 2011 3:57 pm 
Newbie

Joined: Fri Mar 03, 2006 6:03 am
Posts: 8
Hi all,

I have asked this in the Seam forum but nobody seems to be interested. And this may as well be the correct place to post anyway.
This is a Seam based web-application using Hibernate as the JPA provider.

I have a database table that is updated in a highly concurrent manner. Further, the updates are done via different asynchronous event handlers.

I have put a version property in the entity class to get optimistic lock exceptions in order not to have any lost updates. I have read about this in a lot of places including the documentation but instead of retrying the update operation after getting an optimistic lock, I am trying to avoid those exceptions completely by properly synchronizing the Java code that updates the table.

So for example a single thread calls below method to update an entity instance:
Code:
public void updateMethod(Long id) {
    synchronized(GLOBAL_LOCK) {
        EntityManager entityManager = (EntityManager) Component.getInstance("entityManager");
        SomeEntity entity = entityManager.find(SomeEntity.class, id);
        entityManager.refresh(entity);
        entityManager.lock(entity, LockModeType.WRITE);
        // do some stuff to modify the entity
        entityManager.flush();
    }
}


Here even though I am synchronizing the method on a global level (lock is a singleton per JVM), I get javax.persistence.OptimisticLockException. - javax.persistence.OptimisticLockException: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

I am simply wondering how this can happen even though the EntityManager objects and the underlying transactions are different in different threads. I checked this by debugging and verified it: they are different in different Asynchronous Seam events. Since the transactions and entityManager's are different instances and the code is mutually exclusive, I shouldn't be hitting any OptimisticLockException's right? Any ideas why this is happening? How come the entityManager gets an outdated version of the entity even though it is refreshed, flushed, synchronized, etc...? Where does it get the outdated entity from? Cache or something?

I have tested with entityManager.close() added after the flush() but that didn't help either.

Any feedback is appreciated.

Regards, Bora.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.