Hibernate 再接触 悲观锁和乐观锁

Posted Blueburble

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate 再接触 悲观锁和乐观锁相关的知识,希望对你有一定的参考价值。

为什么取1248

二进制

CRUD

移位效率高

 

在并发和效率选择一个平衡点

一般不会考虑幻读 因为我们不会再一个事务里查询两次,(只能设置为seralizable)

悲观锁和乐观锁的前提是read-uncommitted

在数据库中 默认是repeatable read

悲观锁是想着总有人要更改 所以使用数据库的锁

乐观锁是在程序级别的 设置一个版本号 如果前后不一致就进行自己的操作

例子

悲观所

Acocount

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Account {
    private int id;
    private int balance; //BigDecimal
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getBalance() {
        return balance;
    }
    public void setBalance(int balance) {
        this.balance = balance;
    }
    
    
}

Test

    @Test
    public void testSave() {
        Session session = sf.openSession();
        session.beginTransaction();
        
        Account a = new Account();
        a.setBalance(100);
        session.save(a);
            
        session.getTransaction().commit();
        session.close();
    }
    
    @Test
    public void testOperation1() {
        Session session = sf.openSession();
        session.beginTransaction();
        
        Account a = (Account)session.load(Account.class, 1);
        int balance = a.getBalance();
        //do some caculations
        balance = balance - 10;
        a.setBalance(balance);
        session.getTransaction().commit();
        session.close();
    }
    
    @Test
    public void testPessimisticLock() {
        Session session = sf.openSession();
        session.beginTransaction();
        
        Account a = (Account)session.load(Account.class, 1, LockMode.UPGRADE);   //设置数据库锁 不让其他事务访问
        int balance = a.getBalance();
        //do some caculation
        balance = balance - 10;
        a.setBalance(balance);
        session.getTransaction().commit();
        session.close();
    }
    

乐观锁

Account

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Version;          //注意:Version 不用自己设置

@Entity
public class Account {
    private int id;
    private int balance;
    private int version;
    @Version
    public int getVersion() {
        return version;
    }
    public void setVersion(int version) {
        this.version = version;
    }
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getBalance() {
        return balance;
    }
    public void setBalance(int balance) {
        this.balance = balance;
    }
    
    
}
    @Test
    public void testSchemaExport() {
        new SchemaExport(new AnnotationConfiguration().configure()).create(
                false, true);
    }

    @Test
    public void testSave() {
        Session session = sf.openSession();
        session.beginTransaction();

        Account a = new Account();
        a.setBalance(100);
        session.save(a);

        session.getTransaction().commit();
        session.close();
    }

    @Test
    public void testOptimisticLock() {
        Session session = sf.openSession();

        Session session2 = sf.openSession();

        
        
        
        session.beginTransaction();
        Account a1 = (Account) session.load(Account.class, 1);
        

        session2.beginTransaction();
        Account a2 = (Account) session2.load(Account.class, 1);   //Version不用自己设置
        
        a1.setBalance(900);
        a2.setBalance(1100);

        session.getTransaction().commit();
        System.out.println(a1.getVersion());

        session2.getTransaction().commit();
        System.out.println(a2.getVersion());

        session.close();
        session2.close();
    }

 

以上是关于Hibernate 再接触 悲观锁和乐观锁的主要内容,如果未能解决你的问题,请参考以下文章

hibernate基础24:乐观锁和悲观锁

悲观锁和乐观锁

Hibernate乐观锁悲观锁和多态

悲观锁和乐观锁

悲观锁和乐观锁

乐观锁和悲观锁