-->
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.  [ 6 posts ] 
Author Message
 Post subject: What is the best way to atomically update entities?
PostPosted: Sun May 06, 2007 8:40 am 
Newbie

Joined: Thu Apr 26, 2007 4:15 am
Posts: 9
Hi,

I have a scenario in which I must atomically update an entity in the database (using JPA). The update can be performed concurrently by multiple threads. What is the best approach? (we don't use optimistic locking).

In a simple approach I'd fetch the entity and update it. But I know that EntityManager.find() can return cached entitites instead of fetching the object from the DB. Will EntityManager.createQuery().getSingleResult() always returns fresh results from the DB? (I can't find the place where it is documented). It looks like using createQuery() with pessimistic locking (select for update) is the solution. Am I right?

Thanks for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 10:41 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
From what I understand, you just need to use transactions :-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 12:20 pm 
Newbie

Joined: Thu Apr 26, 2007 4:15 am
Posts: 9
Hi Emmanuel,
Thanks for the reply, but I already use transactions around this "critical section" and I experience concurrent modification problems.

If I have two threads, each running in a transaction, fetch the same entry and when the transactions end, both trying to commit the modified object to the DB.

If both threads hold the same Java object it would require using object synchronization (does it work well in jboss?). If they are different objects, how can hibernate detect the concurrent modification and allow the first transaction to commit and the second transaction to fail (we don't use optimistic locks with versioning)?

Genady


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 1:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
First of all, EntityManager is not thread safe and that's on purpose, so don't try and share it across thread. sharing entities objects instances across thread make few sense too.

In understand what you want now, either:
- change the tx isolation level to REPEATABLE_READ
- use em.lock(object, LockMode.READ)

to ensure this constraint.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 2:06 pm 
Newbie

Joined: Thu Apr 26, 2007 4:15 am
Posts: 9
I use MDBs + ejb3 injections to access entitymanager in different threads.

I will try these techniques. Can you add a few words about how each is implemented? (select for update?)

Thanks!
Genady


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 15, 2007 12:20 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Well,
the first one depends on your database
the second one for hibernate, use Select for update or the Db equivalent when loading the entity

_________________
Emmanuel


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

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.