Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.1.3, Java: 1.4
I've added row locking to a method to prevent two users updating the same row at the same time by adding UPGRADE_NOWAIT to the get before the save.
Code:
public void saveBatch(final Batch batch) throws FunctionalException, SystemException {
if (batch.getBatchId() != null) {
Batch checkUpdate = null;
try {
checkUpdate = (Batch) getHibernateTemplate().get(Batch.class, batch.getBatchId(), LockMode.UPGRADE_NOWAIT);
} catch (CannotAcquireLockException e) {
throw new FunctionalException(LOCATION, Constants.Errors.CONFLICT_UPDATE, Constants.Errors.Errors_Code.CONFLICT_UPDATE);
}
if (checkUpdate == null) {
throw new FunctionalException(LOCATION, Constants.Errors.OBJECT_HAS_ALREADY_BEEN_DELETED,
Constants.Errors.Errors_Code.OBJECT_HAS_ALREADY_BEEN_DELETED);
}
if (!(checkUpdate.getLastModifiedOn().equals(batch.getLastModifiedOn()))) {
throw new FunctionalException(LOCATION, Constants.Errors.CONFLICT_UPDATE, Constants.Errors.Errors_Code.CONFLICT_UPDATE);
}
}
getHibernateTemplate().saveOrUpdate(batch);
}
I'd like to write a unit test for this, but can't work out the best way to mimic two users trying to save at the same time.
Has anyone tried this and can give me any pointers please?