Hibernate(16)什么是Hibernate的乐观锁?

Hibernate的乐观锁(Optimistic Locking)是一种并发控制机制,用于防止在多个事务并发访问相同数据时出现数据冲突。乐观锁的基本思想是,每次读取数据时不进行实际的加锁操作,而是在提交更新时检查数据是否已经被其他事务修改。如果数据在此期间被其他事务修改,则当前事务会回滚并重新尝试。

乐观锁的实现

乐观锁通常通过版本号(version)字段实现。每次更新数据时,都会检查版本号是否一致,并将版本号加1。

示例代码

下面是一个完整的示例,展示如何在Hibernate中使用乐观锁。

配置文件 hibernate.cfg.xml

xml 复制代码
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- 数据库连接配置 -->
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/your_database</property>
        <property name="hibernate.connection.username">your_username</property>
        <property name="hibernate.connection.password">your_password</property>

        <!-- Hibernate 属性配置 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- 映射类配置 -->
        <mapping class="com.example.domain.Student"/>
    </session-factory>
</hibernate-configuration>

HibernateUtil 类

java 复制代码
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static final SessionFactory sessionFactory;

    static {
        try {
            // 从配置文件创建SessionFactory
            sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
        } catch (Throwable ex) {
            // 记录启动失败的错误
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

实体类 Student

java 复制代码
package com.example.domain;

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

@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String name;
    private int age;

    @Version
    private int version;

    public Student() {}

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // getters 和 setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }
}

乐观锁示例代码

下面的示例展示了如何在并发环境中使用乐观锁。

java 复制代码
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HibernateOptimisticLockingExample {
    public static void main(String[] args) {
        // 获取SessionFactory
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        
        // 第一个会话
        Session session1 = sessionFactory.openSession();
        Transaction transaction1 = session1.beginTransaction();
        
        // 第二个会话
        Session session2 = sessionFactory.openSession();
        Transaction transaction2 = session2.beginTransaction();

        try {
            // 在第一个会话中获取对象
            Student student1 = session1.get(Student.class, 1L);
            System.out.println("Session1 - Student: " + student1.getName() + ", Age: " + student1.getAge() + ", Version: " + student1.getVersion());

            // 在第二个会话中获取相同的对象
            Student student2 = session2.get(Student.class, 1L);
            System.out.println("Session2 - Student: " + student2.getName() + ", Age: " + student2.getAge() + ", Version: " + student2.getVersion());

            // 修改并更新对象
            student1.setAge(21);
            session1.update(student1);
            transaction1.commit();
            System.out.println("Session1 - Updated Student: " + student1.getName() + ", Age: " + student1.getAge() + ", Version: " + student1.getVersion());

            // 尝试在第二个会话中提交修改
            student2.setAge(22);
            session2.update(student2);
            transaction2.commit(); // 这里会抛出异常,因为版本号不一致

        } catch (Exception e) {
            if (transaction2 != null) {
                transaction2.rollback();
            }
            e.printStackTrace();
        } finally {
            if (session1 != null) {
                session1.close();
            }
            if (session2 != null) {
                session2.close();
            }
        }

        // 关闭SessionFactory
        sessionFactory.close();
    }
}

详细解释

  1. 配置乐观锁 :在实体类中添加一个@Version注解的字段,通常为整数类型。

    java 复制代码
    @Version
    private int version;
  2. 示例场景

    • 在第一个会话中获取同一个Student对象,修改其属性并提交更新。
    • 在第二个会话中获取同一个Student对象,也修改其属性并试图提交更新。
    • 由于版本号不一致,第二个会话在提交更新时会抛出异常(org.hibernate.StaleObjectStateException)。
  3. 事务管理:在事务中进行数据操作,并在操作完成后提交事务。如果操作失败,则回滚事务以确保数据一致性。

总结

乐观锁是一种有效的并发控制机制,通过版本号字段可以防止多个事务并发修改相同数据时出现数据冲突。在Hibernate中,通过@Version注解可以非常方便地实现乐观锁。理解并正确应用乐观锁,可以有效地提高应用的并发处理能力和数据一致性。

相关推荐
invicinble1 天前
对于springboot
java·spring boot·后端
码界奇点1 天前
基于Spring Boot与Vue的校园后台管理系统设计与实现
vue.js·spring boot·后端·毕业设计·源代码管理
爱编程的小庄1 天前
Rust 发行版本及工具介绍
开发语言·后端·rust
Apifox.1 天前
测试用例越堆越多?用 Apifox 测试套件让自动化回归更易维护
运维·前端·后端·测试工具·单元测试·自动化·测试用例
sunnyday04261 天前
Nginx与Spring Cloud Gateway QPS统计全攻略
java·spring boot·后端·nginx
康王有点困1 天前
Link入门
后端·flink
海南java第二人1 天前
Spring Boot全局异常处理终极指南:打造优雅的API错误响应体系
java·spring boot·后端
小楼v1 天前
消息队列的核心概念与应用(RabbitMQ快速入门)
java·后端·消息队列·rabbitmq·死信队列·交换机·安装步骤
小北方城市网1 天前
接口性能优化实战:从秒级到毫秒级
java·spring boot·redis·后端·python·性能优化
鸡蛋豆腐仙子1 天前
Spring的AOP失效场景
java·后端·spring