日期:2014-05-19 浏览次数:20745 次
Select a.version....from Account as a where (where condition..) Update Account set version = version+1.....(another field) where version =?...(another contidition)?? 如果update的更新结果的行数为0,说明实体从加载以来已经被其他事务修改过,会抛出自定义的乐观锁
....... int rowsUpdated = statement.executeUpdate(sql); If(rowsUpdated= =0){ throws new OptimisticLockingFailureException(); } ........?? hibernate 实现悲观锁:
public class Account{ Long id ; ....... @Version //也可以采用XML文件进行配置 Int version ....... }?Ⅱ.时间戳(timestamps):
Select * from Account where ...(where condition).. for update.?Hibernate实现悲观锁:
....... session.lock(account, LockMode.UPGRADE); ......?或者可以采用如下方法加载对象:
session.get(Account.class,identity,LockMode.UPGRADE).?