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;}

要注意:

  1. 每个class只能有一个@Version标注的属性。
  2. @Version标注的属性应该属于该class的primary table(主表)。
  3. @Version标注的属性的类型包括:int, Integer, short, Short, long, Long, java.sql.Timestamp
  4. 程序不应该手动修改@Version标注的属性(交由框架自动处理)。

本条目发布于2013/06/09。属于ROO分类,被贴了 JPAoptimistic 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的更多相关文章

  1. WebGIS中以version方式实现代码更新后前端自动读取更新代码的方法

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1. 前言 GIS代码进行更新后,由于用户前端已有缓存,导致更新的功能不 ...

  2. iOS项目中的version和build

    Version在plist文件中的key是“CFBundleShortVersionString”,标识应用程序的发布版本号,和AppStore上的版本号保持一致.该版本的版本号是三个分隔的整数组成的 ...

  3. ubuntu系统中java -version所显示的版本与/etc/profile中配置的$JAVA_HOME的关系

    问题产生 ubuntu 18.04环境下,执行 java -version 发现与/etc/profile中的$JAVA_HOME所设置的java版本不同. 推测原因 最近用apt install 安 ...

  4. iOS 在系统设置中展示Version, Build, Git等信息

     在设置中,展示自定义内容,如下图INFO区域内容:         步骤: 1.在项目中添加Settings.bundle文件 Root.plist和Root.plist的Source code如下 ...

  5. 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... ...

  6. Maven中使用<version>LATEST</version>自动依赖最新版本引发的问题

    今天在打包项目的过程中出现了编译问题,奇怪的是这个项目已经好久没有修改过了,报错如下. 找不到符号 [ERROR] 符号: 方法 intent(java.lang.String) [ERROR] 位置 ...

  7. Xcode中的Version和Build的区别

    Version( 应用程序发布版本号 ) Version对应的是CFBundleShortVersionString. Version 一般由产品部门确定,版本号是由分隔的整数组成的字符串,一般有2段 ...

  8. IDEA中的version control问题

    项目已经添加了svn,但右键项目时找不到Svn选择.但在VCS中却有,很奇怪. 这个问题是svn的根路径与当前IDEA打开的项目路径不一致的原因. 在IdeaProjects下有两个项目,一个inju ...

  9. 关于jdk环境变量配置成了1.6.0_39 32位jdk 的路径 cmd中java -version却还是显示 64位或者其他jdk 路径的解决方法

    其实是c盘或者其他盘的 jdk 安装的太多了,把其他的都卸载掉就行了

随机推荐

  1. JavaScript和JSP的区别?

    名字: JS:JavaScript JSP:Java Server Pages 执行过程:JSP先翻译,翻译成Servlet执行 如: test.jsp 要变成 test_jsp.java 然后编译成 ...

  2. SQL2008″Unable to read the list of previously registered servers on this system”

    打开SQL2008,弹出”Unable to read the list of previously registered servers on this system”错误, 微软官方的解决方法:h ...

  3. 华为S5300系列升级固件S5300SI-V100R003C00SPC301.cc

    这个固件是升级V100R005的中间件,所以是必经的,注意,这个固件带有http的服务,但现在无论官网还是外面,几乎找不到这个固件的web功能,如果非要实现,可以拿V100R005的web文件改名为w ...

  4. [置顶] Spring的自动装配

    采用构造函数注入,以及setter方法注入都需要写大量的XML配置文件,这时可以采用另一种方式,就是自动装,由Spring来给我们自动装配我们的Bean. Spring提供了四种自动装配类型 1:By ...

  5. 在Win7环境下安装启动Linux

    在Win7环境下安装启动Linux 在Win7系统下,安装启动Linux特别的不方便,由于XP下的boot.ini配置文件不在了,要加入�一下启动选项的话, 仅仅能使用专门的工具,这或多或少给人带来不 ...

  6. jQuery闭包之浅见,从面向对象角度来理解

    本篇的标题虽然是"jQuery闭包之浅见...",但实际上本篇涉及的多半是javascript"闭包"相关内容,之所以写这个标题,完全是因为自己平常用jQuer ...

  7. OLEDB Excel 与C# 的数据流通方法

    一.             名词解释: OleDbCommand 是对数据源执行各种操作的SQL语句或者存储过程,连接access.excel等数据文件进行数据操作时候用到的,其实和sqlcomma ...

  8. Extjs 事件监听

    <!DOCTYPE html> <html> <head> <title>hello-extjs</title> <meta http ...

  9. SRM 624 D2L3: GameOfSegments, 博弈论,Sprague–Grundy theorem,Nimber

    题目:http://community.topcoder.com/stat?c=problem_statement&pm=13204&rd=15857 这道题目须要用到博弈论中的经典理 ...

  10. KStudio window上编译uclinux

     可能没有几个人能像我这样在Windows下编译Linux内核,甚至于同时进行内核调试.这种事情我不是第一个做到的,至少我们公司的原TKStudio部门已经做过.在TKStudio网站上,提供了一个L ...