数据库事务:验证隔离级别-脏读

说明

本文中的代码,是对 数据库事务:脏读、不可重复读、幻读 中 脏读 的验证,所以需要先看该文中的脏读

注意:为了更好的理解代码,代码尽量简洁,所以 不严谨

插入数据类

package test;

import java.sql.Connection;
import java.sql.SQLException;

public class S1脏读1_插入数据 {

    public static void main(String[] args) throws SQLException {
        String driver = "com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        Connection connection = null;
        String sql = "insert into user ( id,username,password ) values (  3 , 'lucy' , '123456'  )";

        try {
            connection = Db.connect(driver,url,"root","root");

            // 不自动提交
            connection.setAutoCommit(false);
            int n = Db.update(connection,sql);

            System.out.println("休眠10秒");
            Thread.sleep(10000);

            System.out.println("执行报错");
            int x = 1/0;

            System.out.println("提交事务");
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("回滚事务");
            connection.rollback();
        }finally {
            if(connection!=null){
                connection.close();
            }
        }
    }
}

查询数据类

有两个 main() 方法:

  • 第一个:查询时,启用事务,事务隔离级别设置 读未提交,可查询到 未提交的数据

  • 第二个:查询时,启用事务,事务隔离级别设置 读提交,不会 查询到 未提交的数据

package test;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class S1脏读2_查询 {

    /**
     * 查询时,启用事务,事务隔离级别设置 读未提交,可查询到未提交的数据
     * @param args
     */
    public static void main(String[] args) throws SQLException {
        String driver = "com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        Connection connection = null;
        try {
            connection = Db.connect(driver,url,"root","root");
            // XXXXX 设置事务隔离级别为:读未提交 XXXXXXXX
            connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
            // 启用事务
            connection.setAutoCommit(false);

            List<Map> list = Db.query(connection,"select id,username,password from user ");
            System.out.println(list);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(connection!=null){
                connection.close();
            }
        }
    }

    /**
     * 查询时,启用事务,事务隔离级别设置 读提交,不会查询到未提交的数据
     * @param args
     */
    public static void main2(String[] args) throws SQLException {
        String driver = "com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        Connection connection = null;
        try {
            connection = Db.connect(driver,url,"root","root");
            // XXXXX 设置事务隔离级别为:读提交 XXXXXXXX
            connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            // 启用事务
            connection.setAutoCommit(false);

            List<Map> list = Db.query(connection,"select id,username,password from user ");
            System.out.println(list);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(connection!=null){
                connection.close();
            }
        }
    }
}

原文出处:/show_1IX78EghoBh3.html