-->
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 synchronization issues
PostPosted: Thu Jun 09, 2005 9:13 am 
Newbie

Joined: Thu Jun 09, 2005 8:41 am
Posts: 2
Location: India
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

[b]Hibernate version: 3.0[/b]

[b]MySql 4.1.8[/b]

[b]The generated SQL (show_sql=false):[/b]

Hi I am developing an application using Hibernate 3.0 and MySql 4.1.8. This application is to be accessed simultaneously(concurrent access) by multiple users.
These multiple users access and updates certain common rows from the database in different sessions.
So, the problem is that application is accessing same bussiness object(database identity) in different session concurrently. Hence the JVM identity of the two instances will be actually different. This causes inconsistencies while updating certain fields for that business object, like say count.

Let a given database row is aceessed concurrently by two users,
User A creates Obj1 in session1 and User B creates Obj2 in session2 . Both Obj1 and Obj2 represent the same row in the database. Let at the time of loading initial count is 45 for both.
Let it is incremented when user finishes and updates this Object with database. As there are two accesses to this object hence it should increase by two.

count should increase by two but actually it is only increasing by 1.

Is there a standard way to avoid this in hibernate. So, that I can use same business object(database identity) in different sessions but still the results should be consistent. Or I have to deal with this at application level.

I can solve this by making a class which contains a static synchronized method used for every access that will write to any object in the database. But, this will serailize whole the update operation on the database. Is there any other way out.


Top
 Profile  
 
 Post subject: Re: Hibernate synchronization issues
PostPosted: Thu Jun 09, 2005 9:19 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Ashish Duggal wrote:
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0

MySql 4.1.8

The generated SQL (show_sql=false):

Hi I am developing an application using Hibernate 3.0 and MySql 4.1.8. This application is to be accessed simultaneously(concurrent access) by multiple users.
These multiple users access and updates certain common rows from the database in different sessions.
So, the problem is that application is accessing same bussiness object(database identity) in different session concurrently. Hence the JVM identity of the two instances will be actually different. This causes inconsistencies while updating certain fields for that business object, like say count.

Let a given database row is aceessed concurrently by two users,
User A creates Obj1 in session1 and User B creates Obj2 in session2 . Both Obj1 and Obj2 represent the same row in the database. Let at the time of loading initial count is 45 for both.
Let it is incremented when user finishes and updates this Object with database. As there are two accesses to this object hence it should increase by two.

count should increase by two but actually it is only increasing by 1.

Is there a standard way to avoid this in hibernate. So, that I can use same business object(database identity) in different sessions but still the results should be consistent. Or I have to deal with this at application level.

I can solve this by making a class which contains a static synchronized method used for every access that will write to any object in the database. But, this will serailize whole the update operation on the database. Is there any other way out.


You should use an Optimistic Locking strategy, with either a timestamp or version column (see docs), and that will ensure that an object read in session2, but later updated in session1, doesn't overwrite the s1 changes when updated in s2. It will instead throw a StaleObjectStateException.


Top
 Profile  
 
 Post subject: synchronization issues Hibernate
PostPosted: Fri Jun 10, 2005 2:41 am 
Newbie

Joined: Thu Jun 09, 2005 8:41 am
Posts: 2
Location: India
Hi, thanks for the prompt reply. Now I can use versioning in my application. But, One more question I would like to ask.
In my application I have to use sometimes direct JDBC also. Is there a way to solve this type of problem in JDBC or I have to implement my own versioning scheme for JDBC.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 1:02 pm 
Newbie

Joined: Wed Apr 20, 2005 10:57 pm
Posts: 6
Hi

Just have a version property. To your table add a Column VERSION. Whenever you update an object, hibernate updates the version column incrementing the integer field.

If you use jdbc to access , then when you read your object note the version number and when you are ready to update check for the version by reading the object from the database and if the version number doesn't match throw an objectstale exception.

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
<class
name="com.xxx.User"
table="XC_USER"
>

<id
name="id"
column="USER_ID"
type="java.lang.Long"
unsaved-value="-1"
>
<generator class="native">
</generator>
</id>

<version
name="version"
type="int"
column="VERSION"
access="property"
unsaved-value="undefined"
/>

<property
name="email"
type="string"
column="EMAIL"
length="128"
not-null="true"
unique="true"
/>

<property
name="name"
type="string"
column="NAME"
length="128"
not-null="false"
unique="false"
/>

<property
name="password"
type="string"
column="PASSWD"
length="80"
not-null="false"
unique="false"
/>


</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 11:35 pm 
Newbie

Joined: Wed Jul 20, 2005 11:23 pm
Posts: 2
any idea in how we retry the operation after we catch the staleobjectdataexception ?


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.