-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate synchronisation and atomicity problem
PostPosted: Fri Nov 23, 2007 2:39 pm 
Newbie

Joined: Fri Nov 23, 2007 2:18 pm
Posts: 3
Hi,
I encounter some trouble using Hibernate.
I use a table "User" and I have 2 access in parallel (using Web Services) on the same entry in my musql DB table.
Each method is useed to modify a different field (data1 and data2) in my entry.
I have the following :
Methode1
User user = get(user);
try {
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

user.setData1(data1);

session.update(user);

transaction.commit();
session.close();

} catch (HibernateException e) {
e.printStackTrace();
}

Methode2
User user = get(user);
try {
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

user.setData2(data2);

session.update(user);

transaction.commit();
session.close();

} catch (HibernateException e) {
e.printStackTrace();
}

The matter is that my 2 accesses occur at the same time and my objects "User" are not synchronized. An atomicity problem.
My 2 processes call the getUser method first (query select on my DB) at the same time. Then if the data1 is modified by the first process, this modification isn't taken into account and is lost by the second process during the data2 processing.
What can be done ?

Two other questions :
- How can I confgure Hibernate to update only the field (or the colonn) I have modified in my entry ?

- How can I perform an update request like that :
String hqlUpdate = "update User u set u.data1 = :mydata1 where u.name = :name";
int updatedEntities = s.createQuery( hqlUpdate )
.setString( "name", name )
.setString( "mydata1", mydata1 )
.executeUpdate();
tx.commit();
session.close();
I found 1 entity but it doesn't commit the data.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 25, 2007 7:01 am 
Newbie

Joined: Fri Nov 23, 2007 2:18 pm
Posts: 3
An idea ?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 25, 2007 9:21 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Try enabling dynamic-update and using "dirty" optimistic locking:
Code:
<class name="User" table="test_user" dynamic-update="true"  optimistic-lock="dirty">
....

Hibernate will only update fields that have changed. Be aware that when the _same_ field is changed by both threads hibernate will throw a StaleObjectStateException on one.

I tried your update code, it works perfectly for me.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 3:49 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
>> Each method is useed to modify a different field (data1 and data2) in my entry.

Since we update different columns from 2 sessions, we need to use
<optimistic-lock="all">

------------------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 27, 2007 4:04 pm 
Newbie

Joined: Fri Nov 23, 2007 2:18 pm
Posts: 3
Ok, thanks.
It stills doesn't work.

1. I use an hibernate.cfg.xml file.
My database has already been created without the optimistic-lock option and populated... so my question is the following : will the <property name="hibernate.hbm2ddl.auto">update</property> property allows to configure the dynamic-update="true" optimistic-lock="dirty" options or must I use a <property name="hibernate.hbm2ddl.auto">update</property> to do so ?
I tried these options in my dev environment using HSQL but it doesn't work with "dirty" and "all".
An idea ?
I need to try it with mysql and oracle.

2. Have you an idea why the following doesn't work :
String hqlUpdate = "update User u set u.data1 = :mydata1 where u.name = :name";
How can I make an update SQL request ?

3. Last question, I tried to use a session.lock(user, LockMode.UPGRADE) that doesn't work too. Can it be linked to the fact that I change the session for each request ?

Thanks a lot.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.