Reputation: 1721
I have a servlet code which is using Hibernate for the database transactions. Now, I have code like,
tx = session.beginTransaction();
// codes
session.save();
tx.commit();
Now, I want to lock the table (lock for both read and write) while the transaction codes is executing. Can anybody help me how to do this ?
Upvotes: 1
Views: 234
Reputation: 2281
Try Following piece of code
LockOptions lockOptions = new LoackOptions();
lockOptions.setLockMode(LockMode.READ);
lockOptions.setTimeOut(2000); // number of milliseconds
lockOptions.setScope(false); // set this is to true of you want cascading of the lock to associations.
session.buildLockRequest().lock(objectTobeLocked);
Click here for other options of LockMode Hope this helps.
Upvotes: 1