-->
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.  [ 8 posts ] 
Author Message
 Post subject: Some questions on concurrency
PostPosted: Wed Oct 15, 2003 10:23 am 
Newbie

Joined: Wed Oct 15, 2003 9:48 am
Posts: 2
Location: Montr
Hi,

My team and myself are currently getting familiar with the ORM world and we surely begin to appreciate what Hibernate has to offer.

We're currently testing Hibernate for a JSP-Struts-Hibernate application (with all the proper encapsulating layers between these three components). On of the actions this application needs to accomplish is the edition of reference relational tables (which all have primary keys generated through sequences). The idea is to:

1) Display all the objects (basically, records) from a given table (basically, perform the equivalent of a "select * from TABLE").
2) Have a user select one of these objects to update/delete it.
3) Persist the changes in the database.

Pretty basic so far. One difficulty we have comes when concurrency is involved. As for example, we encounter a situation where two users (UserA and UserB) work on the same table; they both have the same list displayed in front of them and UserA deletes an object while UserB tries to update it: a simple session.update() called by UserB through Hibernate will not be a good thing since basically it will try to update a record no longer part of the database (and it will not return any exception since basically no exception occured). Do we encounter this situation because we do not use Hibernate properly? Should we rely on pessimistic locking strategies? Before updating an object, should we first make sure that the object still corresponds to a record in the database? Does Hibernate provide usual mechanisms to do so?

The questions above might simply show how new we are with Hibernate (but, hey!, ain't that what a Beginners forum is for?) but any guidelines, references, hints, tips, comments, etc. would be greatly appreciated.

Thanks,

_________________
Alexandre Lepage
Conseiller principal
Solutions en intelligence d'affaires
CGI
courriel: alexandre.lepage@cgi.com
t


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2003 10:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Hibernate should throw an exception in this case, since the JDBC rowcount will be 0, instead of the expected 1. (As long as jdbc batch updates are disabled.)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2003 10:59 am 
Newbie

Joined: Wed Oct 15, 2003 9:48 am
Posts: 2
Location: Montr
gavin wrote:
Hibernate should throw an exception in this case, since the JDBC rowcount will be 0, instead of the expected 1. (As long as jdbc batch updates are disabled.)


But of course! We did overlook the hibernate.jdbc.batch_size property; by setting it to 0, Hibernate indeed throws an exception, which, when caught, can allow us to work on that.

Thank you very much!

_________________
Alexandre Lepage
Conseiller principal
Solutions en intelligence d'affaires
CGI
courriel: alexandre.lepage@cgi.com
t


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 12:55 pm 
Newbie

Joined: Fri Oct 17, 2003 12:40 pm
Posts: 16
I have the slightly different problem with concurrency. When userA deletes the record and userB updates it, I get an error. My problem is when userA updates the record and userB updates the record later, userB overrides the change made by userA. But I should get a StaleObjectError when userB updates the record.
I am using the technique mentioned in the reference document 14.3.2 (Many sessions with automatic versioning). I do use saveOrUpdate, then flush, commit and close. Is there any additional steps or am I doing anything wrong.

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 3:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Optimistic locking is the solution to your problem. (A version column is added to the DB table.
Use the version tag of class (section 4.1.7 of the reference guide).

User A gets the version n,
User B gets the version n
User A updates the object and version is be incremented by Hibernate to n+1
User B updated the object and Hibernate tells that version number n != n+1 => exception is raised and no update is done.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 17, 2003 5:02 pm 
Newbie

Joined: Fri Oct 17, 2003 12:40 pm
Posts: 16
I am trying to implement, Many Sessions with automatic versioning (14.3.2 of reference guide) for optimistic locking. What properties need to be set in the mapping file to implement this technique.
You mentioned the Application version checking (14.3.3 reference guide) technique. But in the reference guide, it is mentioned, that this is the least effective method. So I am trying to implement the 14.3.2 method.
Could you help me, in implementing 14.3.2, I don't know what needs to be done in the mapping file to implement it.

Thank you very much for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 19, 2003 9:50 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You just need a <version> or <timestamp> property.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 19, 2003 4:38 pm 
Regular
Regular

Joined: Fri Sep 05, 2003 12:01 am
Posts: 80
Location: Bogot
Quote:
don't know what needs to be done in the mapping file to implement it.


Don't forget to update your db schemma too... (add a column named version to the table that maps to your entity)

_________________
Mauricio Hern


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