LockType public void lock(LockType lockType) {
try {
entityManager.find(LockEntity.class, lockType, LockModeType.PESSIMISTIC_WRITE);
} catch (Exception e) {
Logger.Error("Failed to find LockEntity by LockType(" + lockType.name() + ") because of DB error.");
Logger.Debug("ConfigDbManagerImpl:lock", e);
DBAlarmHelper.raiseAlarm(e);
throw e;
}
DBAlarmHelper.clearAlarm();
}
try {
entityManager.find(LockEntity.class, lockType, LockModeType.PESSIMISTIC_WRITE);
} catch (Exception e) {
Logger.Error("Failed to find LockEntity by LockType(" + lockType.name() + ") because of DB error.");
Logger.Debug("ConfigDbManagerImpl:lock", e);
DBAlarmHelper.raiseAlarm(e);
throw e;
}
DBAlarmHelper.clearAlarm();
}
public void test() throws Exception {
locker.lock(
LockType.AddMessage );
...........
...........
locker.lock(
...........
...........
}
当第一个线程进入test()方法时, 会调用lock方法,从而使用悲观锁锁住该列。 当第二个线程又进入test()方法时, 也想要尝试会的AddMessage的这列,这是就需要等待第一个线程退出test()方法,才能获得该锁。