innoDB可重复读级别是否可以隔离幻行读

import java.io.*;
import java.sql.*;
import java.util.Properties;

/***
 * 验证innoDB可重复读级别是否可以隔离幻行读
 *
 *
 * 查看隔离级别
 * select @@tx_isolation
 * 或
 * select @@transaction_isolation
 *
 * 建表句子如下
 * CREATE TABLE `test` (
 *   `id` int(11) NOT NULL,
 *   PRIMARY KEY (`id`)
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 *
 * 开两个线,第一个读一次 停10S再读取一次
 * 第一个线程开始后5S开始第二个线程去写入一条
 *
 */
public class Jdbc {
    static Properties properties;

    public static void main(String[] args) throws  Exception {
        new MyThread1().start();
        Thread.sleep(5000);
        new MyThread2().start();
    }

    public static class MyThread1 extends Thread  {
        @Override
        public void run() {
            try {
                Connection connection=getConnection();
                connection.setAutoCommit(false);
                String sql="SELECT COUNT(*) FROM EXERCISE.TEST";
                Statement statement=connection.createStatement();
                System.out.println("读取");
                ResultSet resultSet1=statement.executeQuery(sql);
                while (resultSet1.next()){
                    System.out.println(resultSet1.getString("COUNT(*)"));
                }
                Thread.sleep(10000);
                System.out.println("读取");
                ResultSet resultSet2=statement.executeQuery(sql);
                while (resultSet2.next()){
                    System.out.println(resultSet2.getString("COUNT(*)"));
                }
                connection.commit();
                statement.close();
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static class MyThread2 extends Thread {
        @Override
        public void run() {
            try {
                Connection connection=getConnection();
                connection.setAutoCommit(false);
                String sql="INSERT INTO EXERCISE.TEST VALUES(2);";
                Statement statement=connection.createStatement();
                System.out.println("插入");
                System.out.println(statement.executeUpdate(sql));
                connection.commit();
                statement.close();
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static Connection getConnection() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection(
                properties.getProperty("localhostURL"),
                properties.getProperty("localhostUser"),
                properties.getProperty("localhostPwd")
        );
    }

    static {
        InputStream in = null;
        try {
            in = new FileInputStream(new File("D:\\javaProject\\exercise\\src\\main\\Resources\\db.properties"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        properties = new Properties();
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(mysql)