File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Loading same table row in two different sessions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Loading same table row in two different sessions" Watch "Loading same table row in two different sessions" New topic
Author

Loading same table row in two different sessions

Steve Atkins
Greenhorn

Joined: Jun 20, 2009
Posts: 7
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

Joined: Feb 12, 2009
Posts: 133
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

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Jun 20, 2009
Posts: 7
Thanks john and cameroon for showing the way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading same table row in two different sessions