Hibernate 樂觀鎖(Optimistic Locking)

來源:互聯網
上載者:User

標籤:讀取   ase   swa   nat   throw   comm   vax   extends   hiberna   

  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方法。

Hibernate 樂觀鎖(Optimistic Locking)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.