This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Loading same table row in two different sessions  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

There are two users:
User1 and User2

Both open two different sessions at the same time and load the same table row in memory.
If User1 and User2 both update some columns of that row and suppose User1 commits the transaction first.
What will happen when User2 commits the transaction. Will his changes be overwritten over User1's changes in the database table?
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,

what will happen depends on the locking mechanism you are using (and maybe the OR-Mapping solution).

Basically there are two locking modes: optimistic and pessimistic locking.

When using optimistic locking then the result will be what you describe. When using pessimistic locking the second thread will cause an exception.


You can google for "optimistic locking" or "pessimistic locking" and your OR-Mapping tool and should find a lot of examples.


John
 
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Version attribute can also be helpful in performing optimistic locking:

"Hibernate uses the updated row count to detect conflicting data modifications. Hibernate will automatically disable JDBC batch updates if you are using versioned data, since some JDBC drivers return incorrect values after UPDATEs "

Hibernate Docs

-Cameron McKenzie
 
Steve Atkins
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks john and cameroon for showing the way.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!