TestTransaction1
package test.transaction;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class TestTransaction1 extends BaseTestCase {
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void test1() {
try {
System.out.println(this.getClass().getName());
conn.setAutoCommit(false);
conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
String sql2 = "select item from sys_dbinfo where id =1 ";
PreparedStatement ps2 = conn.prepareStatement(sql2);
ResultSet rs2 = ps2.executeQuery();
rs2.next();
System.out.println(rs2.getString(1));
rs2.close();
System.out.println("======================");
PreparedStatement ps3 = conn.prepareStatement(sql2);
ResultSet rs3 = ps3.executeQuery();
rs3.next();
System.out.println(rs3.getString(1));
rs3.close();
ps3.close();
conn.commit();
System.out.println(this.getClass().getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
用 debug 方式,先让 t1 ,停住,让 t2 完全执行完毕(模拟 2 个事务并发操作),然后让 t1 一行行执行,得到的结果就可以完全验证,从数据中就可以完全看到 2 着的区别与联系

