![](/img/trans.png)
[英]Hibernate Optimistic Lock with/without a Version not working
[英]Optimistic locking without version field
我有一个带有状态字段的表
CREATE TABLE `new_table` (
`id` BIGINT(11) NOT NULL,
`name` varchar(45) NOT NULL,
`value` int(11) NOT NULL DEFAULT '0',
`status` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT;
状态可以具有诸如
DRAFT, POSTED, DELTED
我想实现锁定,以便一次只有一个事务可以更新状态。 我正在使用 java 和 jdbc。 下面是从我的代码转换后的 sql。
start transaction;
update db_example.new_table set value=100, status='POSTED' where name="b" and status='DRAFT';
commit;
代码 :
myRepo.executeInTransaction(()->{
Update update = new Update();
update.set("value", 100);
update.set("status", "POSTED);
Criteria criteria = TSqlUtils.newCriteria("name");
criteria.is("b").and("status").is("DRAFT");
// DO something in another table
...
//
int count = this.mysqlDao.update(this.getJdbcTemplate(), Query.from(criteria), update, MyObject.class);
if(count == 0){
throw new TException("Conflicting transaction");
}
});
如果我只根据状态检查,是否足以锁定?
看起来您首先需要决定业务需求。 如果两个线程决定同时更新状态会发生什么?
例如,最后修改器获胜是一个常见的解决方案。 另一种解决方案可能是拒绝第二次修改并将信息性错误返回给第二个进程。
排序后,代码自然会随之而来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.