码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate 再接触 悲观锁和乐观锁

时间:2018-09-07 22:52:48      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:悲观锁   更改   balance   ati   .com   sim   schema   com   为我   

技术分享图片

为什么取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 再接触 悲观锁和乐观锁

标签:悲观锁   更改   balance   ati   .com   sim   schema   com   为我   

原文地址:https://www.cnblogs.com/frankzone/p/9607307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!