论坛首页 入门技术论坛

请教高手关于事务隔离的问题

浏览 1510 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-03-17  
private static String url = "jdbc:mysql://localhost:3306/test";
	private static String username = "root";
	private static String password = "";
	
	
	
	public static Connection  getConn() throws Exception {
		Class.forName("org.gjt.mm.mysql.Driver").newInstance();
		Connection con =  DriverManager.getConnection(url , username , password);
		return con;
	}

public static void demo3() throws Exception{ 
		
		Connection con1  = getConn();
		con1.setAutoCommit(false);
		con1.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);//设置事务的隔离级别
		String sql1 = "Select * from employee where salary=1000";
		Statement stmt1 = con1.createStatement();
		ResultSet rs1 = stmt1.executeQuery(sql1);
		while(rs1.next()){
			System.out.print(rs1.getInt(1) + " ");
			System.out.print(rs1.getString(2) + " ");
			System.out.println(rs1.getString(3));
		}
		stmt1.close();
		
		Connection con2  = getConn();
		con2.setAutoCommit(false);
		String sql2 = "insert into employee(name,salary) values(\"gigi\",1000)";
		Statement stmt2 = con2.createStatement();
		stmt2.executeUpdate(sql2);	
		con2.commit();
		stmt2.close();
		con2.close();
		
		System.out.println("===========================");
		Statement stmt11 = con1.createStatement();
		ResultSet rs11 = stmt11.executeQuery(sql1);
		while(rs11.next()){
			System.out.print(rs11.getInt(1) + " ");
			System.out.print(rs11.getString(2) + " ");
			System.out.println(rs11.getString(3));
		}
		stmt11.close();
		con1.commit();
		con1.close();
	}

	public static void main(String[] args) throws Exception {
		
		//demo1();
		//demo2();
		demo3();
	}


实际打印结果为:
1 gigi 1000
===========================
1 gigi 1000

预期的结果应该是:
1 gigi 1000
===========================
1 gigi 1000
2 gigi 1000

请高手 帮我看看这个代码.为啥隔离级别设置成ISOLATION_REPEATABLE_READ了,却不能幻像读
按照文档
ISOLATION_READ_UNCOMMITTED
ISOLATION_READ_COMMITTED
ISOLATION_REPEATABLE_READ 都是应该允许幻像读的
我用的是 mysql 的 InnoDB 存储引擎 .文档上说支持4个标准的事务隔离
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics