zoukankan      html  css  js  c++  java
  • Hibernate 乐观锁(Optimistic Locking)

      1、hibernate基于数据版本(Version)记录机制实现。为数据增加一个版本标识,一般是通过为数据库表增加一个“version”字段来实现。 读取出数据时,将此版本号一同读出,之后更新时,对此版本号加一。此时,将提交数据的版本数据与数据库表对应记录的当前版本信息进行比对,如果提交的数据 版本号大于数据库表当前版本号,则予以更新,否则认为是过期数据。

      2、

     1 //$Id: Conductor.java 11282 2007-03-14 22:05:59Z epbernard $  
     2 package org.hibernate.test.annotations.various;  
     3   
     4 import javax.persistence.Column;  
     5 import javax.persistence.Entity;  
     6 import javax.persistence.GeneratedValue;  
     7 import javax.persistence.Id;  
     8 import javax.persistence.Version;  
     9   
    10 import org.hibernate.annotations.Index;  
    11 import org.hibernate.annotations.OptimisticLock;  
    12   
    13   
    14 @Entity  
    15 public class Conductor {  
    16     @Id  
    17     @GeneratedValue  
    18     private Integer id;  
    19     @Column(name = "cond_name")  
    20     @Index(name = "cond_name")  
    21     <span style="color: rgb(255, 0, 0);">//@OptimisticLock(excluded = true)</span>  
    22   
    23     private String name;  
    24     @Column(name = "cond_address")  
    25     @Index(name = "cond_address")  
    26     <span style="color: rgb(255, 0, 0);">@OptimisticLock(excluded = true)</span>  
    27   
    28     private String address;  
    29     @Version  
    30     private Long version;  
    31   
    32 }  
    33  address是本人加上的.同时省略了get set方法.
    34 测试:
    35 Java代码 
    36 //$Id: IndexTest.java 11282 2007-03-14 22:05:59Z epbernard $  
    37 package org.hibernate.test.annotations.various;  
    38   
    39 ...........................  
    40   
    41   
    42 public class IndexTest extends TestCase {  
    43     ...............  
    44   
    45     @SuppressWarnings("unchecked")  
    46     public void testVersion() throws Exception {  
    47           
    48         Session session1=openSession();  
    49         Session session2=openSession();  
    50         Conductor stu1=(Conductor)session1.createQuery("from Conductor as a where a.name='Bob'").uniqueResult();  
    51         Conductor stu2=(Conductor)session2.createQuery("from Conductor as a where a.name='Bob'").uniqueResult();  
    52           
    53         //这时候,两个版本号是相同的  
    54         System.out.println("v1="+stu1.getVersion()+"--v2="+stu2.getVersion());  
    55           
    56         Transaction tx1=session1.beginTransaction();  
    57         stu1.setName("session1");  
    58         tx1.commit();  
    59         //这时候,两个版本号是不同的,其中一个的版本号递增了  
    60         System.out.println("v1="+stu1.getVersion()+"--v2="+stu2.getVersion());  
    61           
    62         Transaction tx2=session2.beginTransaction();  
    63         stu2.setName("session2");  
    64           
    65         tx2.rollback();  
    66         session2.close();  
    67         session1.close();  
    68     }  
    69     ............  
    70 }  

      添加testVersion方法。

  • 相关阅读:
    临时表空间占用大量空间(新建)
    学习总结
    sql:表关联方式
    11gR2 Clusterware 和 Grid Home
    sql分析常用查询
    通过 SSH 实现 TCP / IP 隧道(端口转发):使用 OpenSSH 可能的 8 种场景
    Fabric部署环境初始化(Centos 7)
    Fabric 学习路线
    代币智能合约(go)
    springboot切面编程范例
  • 原文地址:https://www.cnblogs.com/lcngu/p/6257435.html
Copyright © 2011-2022 走看看