两个线程同时查一行记录,然后更新记录。
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.executeQuery();
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 呢?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.