栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

手动设置版本字段时的乐观锁定不会引发异常

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

手动设置版本字段时的乐观锁定不会引发异常

不幸的是,(至少对于Hibernate而言)

@Version
手动更改字段不会使它成为另一个“版本”。即乐观并发检查是针对读取实体时获取的版本值而不是针对实体更新时的版本字段进行的。

例如

这会工作

Foo foo = fooRepo.findOne(id);  // assume version is 2 herefoo.setSomeField(....);// Assume at this point of time someone else change the record in DB, // and incrementing version in DB to 3fooRepo.flush();  // forcing an update, then Optimistic Concurrency exception will be thrown

但是这不起作用

Foo foo = fooRepo.findOne(id);  // assume version is 2 herefoo.setSomeField(....);foo.setVersion(1);fooRepo.flush();  // forcing an update, no optimistic concurrency exception       // Coz Hibernate is "smart" enough to use the original 2 for comparison

有一些解决方法。最简单的方法可能是自己实施乐观并发检查。我曾经有一个工具来进行“
DTO到模型”数据填充,并且在那里放置了版本检查逻辑。另一种方法是将逻辑放在

setVersion()
其中,而不是真正设置版本,而是执行版本检查:

class User {    private int version = 0;    //.....    public void setVersion(int version) {        if (this.version != version) { throw new YourOwnOptimisticConcurrencyException();        }    }    //.....}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/447564.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号