File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading same table row in two different sessions

 
Steve Atkins
Greenhorn
Posts: 7
  • 0
  • 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?
 
John Bengler
Ranch Hand
Posts: 133
  • 0
  • 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
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks john and cameroon for showing the way.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic