Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simulating select for update in hibernate.

 
Chase Bonham
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a POJO mapped to a table (one primary key) and everytime I do an update to a row of that table I want to do a "select for update" effectively locking
other people from concurrently making the update. Is there a hibernate 2.0
mapping attribute I can use?
 
Ghulam Rashid
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably you have use LockMode feature of Hibernate.


Transaction transaction = session.beginTransaction();
Country country =
(Country) session.get(Country.class, id, LockMode.UPGRADE);
cat.setName("India");
transaction.commit();

Country will be loaded into session using a SELECT FOR UPDATE,
thus locking the retrieved rows in the database until they�re released when the transaction ends.

Hope this will help.

Rashid
 
Ghulam Rashid
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
correction to above code.
country.setName("India");
 
Kent O. Johnson
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this is a bump. However, this post came up for me in a google search for Hibernate transaction locking so here is the updated solution I found:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic