-->
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.  [ 7 posts ] 
Author Message
 Post subject: Concurrent Updates - Question
PostPosted: Fri Jan 26, 2007 4:04 pm 
Newbie

Joined: Wed Jan 24, 2007 2:54 pm
Posts: 6
Hibernate version: 3.2.1 ga

I am opening two sessions in parallel and trying to update the same row in both the sessions. But I am not getting any exception even though I am doing concurrent modifications. Can somebody explain what I am missing here. I went through the 11 chapter in Hibernate tutorial and according to "11.3.2 Extended session and automatic versioning", Hibernate checks instance versions at flush time, throwing an exception if concurrent modification is detected.

Code between sessionFactory.openSession() and session.close():
SessionFactory sessionFactory1 = new Configuration().configure().buildSessionFactory();
SessionFactory sessionFactory2 = new Configuration().configure().buildSessionFactory();

session1 = sessionFactory1.openSession();
session2 = sessionFactory2.openSession();

Transaction tx1 = session1.beginTransaction();
Transaction tx2 = session2.beginTransaction();

Employee employee1 = (Employee) session1.get("com.test.hibernate.Employee", 1);
Employee employee2 = (Employee) session2.get("com.test.hibernate.Employee", 1);

System.out.println("Employee1 Name:" + employee1.getFirstName());
System.out.println("Employee2 Name:" + employee2.getFirstName());

employee1.setFirstName("joe1");
session1.save(employee1);
tx1.commit();

employee2.setFirstName("joe2");
session2.save(employee2);
tx2.commit();

Name and version of the database you are using:
Oracle 10g XE


Last edited by joe_alex on Fri Jan 26, 2007 4:51 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 26, 2007 4:35 pm 
Regular
Regular

Joined: Wed Dec 07, 2005 4:19 pm
Posts: 53
Are your sesion1 and session2 truly different (and are your object instances different)?
Does your object have a 'version'? (mapping file would help).
(ususally, I have the oposite problem = stale data exception).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 26, 2007 4:45 pm 
Regular
Regular

Joined: Wed Dec 07, 2005 4:19 pm
Posts: 53
Oops, I should have noticed that:

Quote:
employee.setFirstName("joe1");
session1.save(employee);
tx1.commit();

employee.setFirstName("joe2");
session2.save(employee);
tx2.commit();

Your session2.save(employee) is saving an object which has NOT been loaded by session2 - it was loaded by session1.
Perhaps you wanted to change and save en 'employee2'.

So, in your code, employee may be considered a 'detached' object (from the session2's point of view). Then the behavior depends great deal upon how you defined your object 'identity' -- I suspect there is something wrong there...

(Don't forget to rate this posting, if it happened to help).
Martin.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 26, 2007 4:54 pm 
Newbie

Joined: Wed Jan 24, 2007 2:54 pm
Posts: 6
Sorry Martin. My mistake. I have corrected the code snippet. Please refer the same.

I am trying to save only employee2.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 26, 2007 5:55 pm 
Regular
Regular

Joined: Wed Dec 07, 2005 4:19 pm
Posts: 53
Well, then it's back to what is your versioning like.
Do you have it in your mapping file?
Is your get/set method for version working?
(assuming employee1 != employee2)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 26, 2007 6:40 pm 
Newbie

Joined: Wed Jan 24, 2007 2:54 pm
Posts: 6
Thanks. Now I looked at the version element. Does it mean that I should add a version column in my tables (and in mapping file) wherever I need to avoid the dirty update?

My mapping is

<hibernate-mapping>
<class name="com.test.hibernate.Employee" table="EMPLOYEE">
<id name="empId" type="int" column="EMP_ID" >
<generator class="increment"/>
</id>
<property name="firstName">
<column name="FIRST_NAME" />
</property>
<property name="experience">
<column name="EXPERIENCE"/>
</property>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 26, 2007 7:45 pm 
Regular
Regular

Joined: Wed Dec 07, 2005 4:19 pm
Posts: 53
Of course. You need property like Integer version in your object, and a column like VERSION int in your table.
Without it, Hibernate has nothing to check for.
Note special mapping for the version element in your mapping file:
<version name="version" type="integer" column="VERSION" />


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