Roo中的@Version
Roo中的@Version
问题提出
当我们为entity添加@RooJpaActiveRecord注解时,Roo为我们自动生成了一个名为Entity_Roo_Jpa_Entity.aj的文件,其中有一句:
@Version@Column(name ="version")privateIntegerEntity.version;
这是什么意思呢?“Version”是版本的意思,那么“@Version”注解是版本号的意思吗?Entity为什么需要版本管理? 下面我们对此展开探寻。
线索
注意到Entity_Roo_Jpa_Entity.aj与@RooJpaActiveRecord有关,由上一篇博客可推测,@Version与JPA有关。打开Entity_Roo_Jpa_Entity.aj文件,鼠标移到@Version注解上,显示:
Specifies the version field or property of an entity class that serves as its optimistic lock value. The version is used to ensure integrity when performing the merge operation and for optimistic concurrency control.
可知,@Version的作用是声明注释的属性或者返回值作为optimistic lock value。version属性的作用是optimistic concurrency control。
Lock
optimistic lock的一般译为“乐观锁”,属于数据库事务并发控制领域的概念。数据库并发控制一般由“Lock(锁)”来实现的。这里提到:
Transactional isolation is usually implemented by locking whatever is accessed in a transaction. There are two different approaches to transactional locking: Pessimistic locking and optimistic locking.
可知transaction的isolation性质(ACID中的I)通常通过lock进行访问控制来实现。lock具体可分为pessimistic lock(悲观锁)和optimistic lock(乐观锁)。
pessimistic lock(悲观锁)
pessimistic lock的意思是:
a resource is locked from the time it is first accessed in a transaction until the transaction is finished, making it inaccessible to other transactions during that time.
也就是说,在一个transaction中,data会被完全锁住,直到transaction结束。另一个transaction若要访问该data,则需要等待。注意pessimistic lock可能会引起循环等待导致死锁。由于pessimistic lock的特性,采取该并法策略可能会导致运行效率大幅度下降,故并不适合高并发、长事务的场景。
optimistic lock(乐观锁)
这里提到:
Optimistic locking does not use exclusive locks when reading. Instead, a check is made during the update to make sure that the record has not been changed since it was read.
这里也提到:
At commit time, when the resource is about to be updated in persistent storage, the state of the resource is read from storage again and compared to the state that was saved when the resource was first accessed in the transaction. If the two states differ, a conflicting update was made, and the transaction will be rolled back.
可以看出optimistic lock的实现原理是通过在准备同步数据库时(commit时)对比数据在transaction中初次读取时和当前在数据库中的状态,看其是否满足某种规则来决定此次同步是否成功,若不成功则rollback。
具体的实现,这里举了一个例子:
一般的应用是采用数据版本的方式(version)实现,在读取数据的时候将version读取出来,在保存数据的时候判断version 的值是否小于数据库中的version的值,小于则不允许更新,否则可以更新。
值得注意的是,optimistic lock的实现并不依赖于数据库,而是基于系统中的数据存储逻辑。故系统外部的更新并不受optimistic lock的控制,可能会造成脏数据被更新到系统当中。
@Version
那么,@Version注解的具体用法是怎么样的呢?结合API文档,如上所述,我们只需要在某个数值属性或者timestamp类型的属性上标注@Version注解即可:
@Version@Column(name ="version")privateIntegerEntity.version;
或者:
@Version@Column(name="version")protectedint getVersion(){return version;}
要注意:
- 每个class只能有一个@Version标注的属性。
- @Version标注的属性应该属于该class的primary table(主表)。
- @Version标注的属性的类型包括:int, Integer, short, Short, long, Long, java.sql.Timestamp
- 程序不应该手动修改@Version标注的属性(交由框架自动处理)。
本条目发布于2013/06/09。属于ROO分类,被贴了 JPA、optimistic lock 标签。
文章导航
← Roo中的ActiveRecord以及EntityManagementRoo中的i18n →
发表评论
电子邮件地址不会被公开。 必填项已用*标注
姓名 *
电子邮件 *
站点
评论
您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Notify me of follow-up comments by email.
Notify me of new posts by email.
NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!
Roo中的@Version的更多相关文章
- WebGIS中以version方式实现代码更新后前端自动读取更新代码的方法
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1. 前言 GIS代码进行更新后,由于用户前端已有缓存,导致更新的功能不 ...
- iOS项目中的version和build
Version在plist文件中的key是“CFBundleShortVersionString”,标识应用程序的发布版本号,和AppStore上的版本号保持一致.该版本的版本号是三个分隔的整数组成的 ...
- ubuntu系统中java -version所显示的版本与/etc/profile中配置的$JAVA_HOME的关系
问题产生 ubuntu 18.04环境下,执行 java -version 发现与/etc/profile中的$JAVA_HOME所设置的java版本不同. 推测原因 最近用apt install 安 ...
- iOS 在系统设置中展示Version, Build, Git等信息
在设置中,展示自定义内容,如下图INFO区域内容: 步骤: 1.在项目中添加Settings.bundle文件 Root.plist和Root.plist的Source code如下 ...
- eclipse中Tomcat version 9.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5, 6, 7, and 8 Web modules
eclipse中导入了一个别人的项目,运行时提示没有可以使用的服务器,如下: 查看了下项目属性设置中的服务器,还是提示没有可用服务器: 尝试对部署在已有服务器下的项目Add and Remove... ...
- Maven中使用<version>LATEST</version>自动依赖最新版本引发的问题
今天在打包项目的过程中出现了编译问题,奇怪的是这个项目已经好久没有修改过了,报错如下. 找不到符号 [ERROR] 符号: 方法 intent(java.lang.String) [ERROR] 位置 ...
- Xcode中的Version和Build的区别
Version( 应用程序发布版本号 ) Version对应的是CFBundleShortVersionString. Version 一般由产品部门确定,版本号是由分隔的整数组成的字符串,一般有2段 ...
- IDEA中的version control问题
项目已经添加了svn,但右键项目时找不到Svn选择.但在VCS中却有,很奇怪. 这个问题是svn的根路径与当前IDEA打开的项目路径不一致的原因. 在IdeaProjects下有两个项目,一个inju ...
- 关于jdk环境变量配置成了1.6.0_39 32位jdk 的路径 cmd中java -version却还是显示 64位或者其他jdk 路径的解决方法
其实是c盘或者其他盘的 jdk 安装的太多了,把其他的都卸载掉就行了
随机推荐
- bzoj 3668 数位DP
收获: 1.如果有很多位操作,并且不包含+-×/等高级运算,那么可以一位一位考虑,如果求一个最优解,可以尝试逐位确定,这道题因为原始攻击值有范围,那么就需要数位DP. /*************** ...
- bzoj 4237: 稻草人 -- CDQ分治
4237: 稻草人 Time Limit: 40 Sec Memory Limit: 256 MB Description JOI村有一片荒地,上面竖着N个稻草人,村民们每年多次在稻草人们的周围举行 ...
- bzoj 1176: [Balkan2007]Mokia&&2683: 简单题 -- cdq分治
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要 ...
- python及其模块下载集合
1)python平台 https://www.python.org/downloads/ 2)打包工具 cx-freeze(python3以上版本打包工具) http://cx-freeze.sour ...
- 2、Redis的基础知识
写在前面的话:读书破万卷,编码如有神 -------------------------------------------------------------------- 主要内容包括: red ...
- ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS
B. Om Nom and Dark Park Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- 基于Landmark的人脸对齐以及裁剪方法
利用Landmarks进行人脸对齐裁剪是人脸检测中重要的一个步骤.效果如下图所示: 基本思路为: a.人脸检测 人脸的检测不必多说了,基本Cascade的方式已经很不错了,或者用基于HOG/FHOG的 ...
- 小程序获取当前页面路径url
getCurrentPages()[0].route
- 如何从Windows远程上传文件到Linux(例如CentOS 7)
一.先看Linux系统是否安装有vsftp软件(vs是very secure的意思) [root@localhost /]# rpm -qa | grep vsftpdvsftpd-3.0.2-9.e ...
- Writable atomic property '***' cannot pair a synthesized setter/getter with a user defined
1. warning: Semantic Issue: Writable atomic property 'number' cannot pair a synthesized setter/gette ...