-->
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: Detecting updates made to underlying tables by another app?
PostPosted: Tue Mar 31, 2009 1:22 am 
Newbie

Joined: Tue Mar 31, 2009 12:55 am
Posts: 2
In Hibernate, how can I detect the changes made to the underlying table/row by an external app/process?

Here's an example of the situation I'm trying to solve:

1. Entity with id=10001 is retrieved in App One.
2. While the entity is in a detached state and being modified in App One, App Two, which is not related to App One, updates the corresponding row in the underlying table.
3. However, the Hibernate in App One doesn't seem to be aware of this change (as expected?), whether I issue another 'load' or 'refresh' just to check if it's able to detect the underlying change, and causes problem/confusion when I try to apply the changes made in App One to the DB.

Is it the caching that's the problem? How is Hibernate designed to handle this kind of synchronization issue?
What is the best approach/way to handle this kind of situation, which is fairly common in a large enterprise environment?

I'm a novice and any help or idea would be appreciated.

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 31, 2009 5:18 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
Can you post your code? It would help if we could see how you were doing it.

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 31, 2009 5:20 pm 
Beginner
Beginner

Joined: Fri May 14, 2004 9:50 am
Posts: 28
Hello,

You have a problem of concurrency access between applications.
You can choose an optimistic concurrency control or a pessimistic one.
The optimistic one will add a version number to your object (table), but your second application has to deal with version number.

http://www.hibernate.org/hib_docs/v3/re ... optimistic

The pessimistic one, lock the line at the table level.

http://www.hibernate.org/hib_docs/v3/re ... ns-locking

_________________
Eric

http://www.viaxoft.com
http://blog.viaxoft.net


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 31, 2009 5:24 pm 
Senior
Senior

Joined: Tue Aug 01, 2006 9:24 pm
Posts: 120
Twenty-one is correct, but I still would like to see your code because refresh should be working.

_________________
Please rate my replies as I need points for all of my questions also.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 4:49 pm 
Newbie

Joined: Tue Mar 31, 2009 12:55 am
Posts: 2
Thank you for the good references.

In my case, the 'pessimistic locking' is not an option as the entity can participate in long 'conversations'.

I did try the optimistic locking using the 'version' attribute.
So far what we have found is that it works perfectly (in terms of catching conflicts during an update) as long as the process that committed the conflicting update was within the same app, going through the same hibernate instance layer.
If the conflicting update/commit was made by an external app/process directly to the table row, Hibernate seems to just hang without any error msgs when I try to update the same record and still shows the 'old' image when I do a 'refresh' on the entity in concern, even though the external app has incremented the version column to reflect the change.

I am also looking at my environment settings just to make sure, but am a bit clueless with strong suspicion with the way caching works, with respect to syncing with external updates (if it's supported). I'm not sure how Hibernate was designed to sync itself with the updates made to the underlying tables via external app/processes, which is not a trivial problem to solve. As it's a prototype, note that I am not using level 2 caching and the connection pooling/level 1 caching are the default ones that comes with the Hibernate 3.x installation.


The code is a bit ugly as I am constantly playing around with the JPA interfaces on top of Hibernate but will post the pure Hibernate code once it's cleaned up.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 02, 2009 2:10 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The problems you have with optimistic locking may be related to the use of the default connection pool and a transaction isolation level that is set to REPEATABLE READ. Here is a post describing similar problems and how we solved them: http://forum.hibernate.org/viewtopic.ph ... 63#2394963


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.