两个线程同时查一行记录,然后更新记录。
class MyRunner implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + ":连接数据库..."); Connection cOnnection= null; try { cOnnection= DriverManager.getConnection(RepeatableRead.DB_URL, RepeatableRead.USER, RepeatableRead.PASS); connection.setAutoCommit(false); connection.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); String querySql = "select * from account where id=?"; PreparedStatement queryStmt = connection.prepareStatement(querySql); queryStmt.setInt(1, 1); ResultSet set = queryStmt.executeQuery(); set.next(); int amount = set.getInt("amount"); System.out.println(Thread.currentThread().getName() + ":Amount=" + amount); Thread.sleep(1000); // 读取了数据,未提交事务。 synchronized (this) { String updateSql = "update account set amount=? where id=1"; PreparedStatement updateStmt = connection.prepareStatement(updateSql); updateStmt.setInt(1, amount + 100); int count = updateStmt.executeUpdate(); if (count > 0) { set = queryStmt.executeQuer(); set.next(); amount = set.getInt("amount"); System.out.println(Thread.currentThread().getName() + ":更新成功...|" + amount); connection.commit(); } } } catch (SQLException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } @Test public void testMyRunner() throws ClassNotFoundException, SQLException, InterruptedException { MyRunner myRunner = new MyRunner(); Thread t1 = new Thread(myRunner, "T1"); Thread t2 = new Thread(myRunner, "T2"); t1.start(); t2.start(); t1.join(); t2.join(); } /* 输出: T1:连接数据库... T2:连接数据库... T2:Amount=277 T1:Amount=277 T2:更新成功...|377 T1:更新成功...|277 T1 为什么还是 277 ? */ 由于隔离级别是 RR,T1 不应该也输出 377 吗?
另外要怎么才能保证最后 amount 得到正确的结果 447 呢?
