樂觀鎖就如同SVN同樣,修改一次,版本號自增1。
若是另外一個線程同時修改,獲取的版本號和修改時的不匹配,就會拋出異常。html
public class Product {
private Long id;
private String name;
private Integer number;// 庫存數量
// 定義爲私有字段
//只能在Product類內部修改, 這裏是直接由Hibernate管理的
private Integer version;
<class name="Product">
<id name="id">
<generator class="native" />
</id>
<!-- 私有字段access="field"配置 -->
<version name="version" access="field" />
<property name="name" />
<property name="number" />
</class>
package com.jege.hibernate.z_optimistic_lock;
import org.hibernate.Session;
import org.hibernate.StaleObjectStateException;
import org.junit.Before;
import org.junit.Test;
public class MainTest {
@Before
public void save() throws Exception {
Product product = new Product();
product.setName("xxxx");
product.setNumber(10);
Session session = HibernateUtils.getSession();
session.beginTransaction();
session.save(product);
session.getTransaction().commit();
session.close();
}
// 模擬2個事務在進行毫秒級別update操做
@Test
public void update() throws Exception {
try {
Session session1 = HibernateUtils.getSession();
Session session2 = HibernateUtils.getSession();
session1.beginTransaction();
session2.beginTransaction();
Product product = (Product) session1.get(Product.class, 1L);
product.setNumber(product.getNumber() - 8);
Product product2 = (Product) session2.get(Product.class, 1L);
product2.setNumber(product2.getNumber() - 5);
session2.update(product2);
session1.update(product);
session1.getTransaction().commit();
session2.getTransaction().commit();
session1.close();
session2.close();
} catch (StaleObjectStateException e) {
Session session3 = HibernateUtils.getSession();
Product product = (Product) session3.get(Product.class, 1L);
System.out.println("庫存已經改變,請從新刷新再次購買:" + product.getNumber());
}
}
}
https://github.com/je-ge/hibernategit
若是以爲個人文章或者代碼對您有幫助,能夠請我喝杯咖啡。
您的支持將鼓勵我繼續創做!謝謝!
github