• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Concurrent updates does not throw any exception

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
Vijay Kashyap
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
version element works. But is you are so fed up try using timestamp as a version check.
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic