my dog learned polymorphism*
The moose likes Object Relational Mapping and the fly likes Concurrent updates does not throw any exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Concurrent updates does not throw any exception" Watch "Concurrent updates does not throw any exception" New topic
Author

Concurrent updates does not throw any exception

Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74
Is there any way to force hibernate to throw exception if 2 concurrent update calls are made on same record? I am using optmistic locking but still fails to get any exception from hibernate.

Can anybody suggest what am I missing? I am using Spring with Hibernate.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Can anybody suggest what am I missing?

Well Hibernate's optimistic locking functionality does work. Can you explain a little more about what your are doing and maybe someone will spot what you have done wrong. In particular, could you show us the code that demonstates this error, show us your mapping files and tell us which database you are using?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74
This is the mapping what I am using. From different hibernate sessions 2 distinct update calls are made:

one updates batch state while another updates batch name but only one of them is successful the other update get lost.





[Edited to fix code tags - Paul Sturrock]
[ June 02, 2006: Message edited by: Paul Sturrock ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Your mapping doesn't seem to have a version element. How are you versioning your object?
[ June 02, 2006: Message edited by: Paul Sturrock ]
Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74
Paul,

I have just pasted the relevent information from my mapping. Here is my versioning mapping.

<version
name="version"
type="long"
column="version_id"
access="property"
unsaved-value="undefined"
/>

Thxs
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Now I'm confused. Is the first mapping you posted exactly what you are using? Or is the version element you've just posted part of it too?

One other thing - you say:

From different hibernate sessions 2 distinct update calls are made

Are you sure the updates are concurrent? Can you show us the code where you make the updates?
Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74
Ok, My fault. Version mapping is a part of earlier mapping, its only that I truncated the long mapping to put only relevent part.

I have a situation where 2 threads are making a select call using getHibernateTemplate(), afterward one of them updates batch state while other updates batch name and make an upate call using

getHibernateTemplate().saveOrUpdate(batchInfo);

Depending upon situations I am loosing one of the call.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You are remembering that Spring favours RuntimeExceptions and all HibernateExceptions will be caught and converted into Spring DataAccessExceptions? So that code will never throw a StaleObjectException, as you would expect if you were using Hibernate on its own and a version conflict occurs.

Are you perhaps not logging the possible RuntimeException?
Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74
I am logging all exception but the strange fact is no exceptions not even run time exceptions are thrown by stale updates.

Another question what happends if I set select-for-update to true does that mean that every select for Batch will lock the corresponding row in table.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Can you show the code/describe the situation that proves this is a concurrent update? There is the possibility that the updates are in series, rather than concurrent.

select-for-update: depends on the database, but this would be used for pessimistic locking, not optimistic.
Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74
Why I am saying its concurrent update because in few cases only one of the two batch name or batch state is getting updated.

Also while debugging my application I changed the version_id value (using direct update sql) after BatchInfo is fetched and when the update happens the table record never gets updated which is correct as the version_id in database for this record is now different but application is not throwing any exceptions.


BatchInfo batchInfo = getBatchInfo call (using hibernate template)

-----> updated version_id manually using direct sql

batchInfo.setName("test");

getHibernateTemplate().saveOrUpdate(batchInfo);

---> batch name doesn't get updated as expected but no exceptions are generated either.


I think its better if I synchronize the 2 call rather relying on Hibernate throwing any exception.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I think its better if I synchronize the 2 call rather relying on Hibernate throwing any exception.

No. Synchronizing database access should not be required; it kind of defeats the purpose of using a data store that happily allows concurrent access when you then restrict the access to one user at a time. Hibernate's optimistic locking support (as I said before) works. And its worked for some time.

Be aware that directly updating the database via SQL may not have the effect you want. Remember the Session acts as a short-lived first level cache.



Also while debugging my application I changed the version_id value (using direct update sql) after BatchInfo is fetched and when the update happens the table record never gets updated which is correct as the version_id in database for this record is now different but application is not throwing any exceptions.

This sounds very suspicious, and makes me think again that the exception is getting "lost" rather than not being thrown. Turn on debugging for Hibernate. Get it to log the SQL it is issuing too. See what's going on under the hood.
[ June 02, 2006: Message edited by: Paul Sturrock ]
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

version element works. But is you are so fed up try using timestamp as a version check.


If you are not laughing at yourself, then you just didn't get the joke.
 
jQuery in Action, 2nd edition
 
subject: Concurrent updates does not throw any exception
 
Similar Threads
Helper - Singleton or Static?
hibernate no row selected
An exception throws in hibernate
Update Single value in a row
Hibernate query is called twice???