I have @version annotation in bean (POJO) class as below and created a version column in database to achieve optimistic locking.
Code:
@Version
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
and also session.lock for the same entity to achieve pessimistic locking.
Code:
Sequence SequenceObj = (Sequence) session.get(
Sequence.class, ID);
session.lock(SequenceObj, LockMode.FORCE);
criteria.add(Restrictions.eq("sequenceName", "Some Name"));
criteria.add(Restrictions.eq("sequence",existingId));
SequenceObj.setSequence(existingId);
session.update(capeSequenceObj);
session.flush();
return existingId;
I am getting org.hibernate.exception.LockAcquisitionException: could not retrieve version: for entity exception and transaction getting rolled back. Can I use both(@version annotation for optimistic locking and session.lock for pessimistic locking) for same entity? if so how?
I have also tried only optimistic locking through @ Version (commented session.lock(SequenceObj, LockMode.FORCE);)but getting org.hibernate.exception.LockAcquisitionException: could not update the object.
The problem is in clustered (multi-node) environment, in single node it is working fine.
So finally I have tried optimistic locking and pessimistic locking at first go and later tried with only optimistic locking but none of the approach worked. Please throw some light as I am not expert in this area.