首页 新闻 会员 周边

try restarting transaction 问题

0
悬赏园豆:20 [已解决问题] 解决于 2017-05-08 22:37

建立一个getConn方法,用两个Connection对象去接,是不是相当于开了两个client,为什么用第二个对象去update数据的时候会报错Lock wait timeout exceeded; try restarting transaction (第一个对象auto commit = false),按我的理解两个对象的话应该是可以出现不可重复读现象的,
代码如下;
 1     public void selectREPEATABLE() {
 2         Connection conn = null ;
 3         PreparedStatement ppst = null ;
 4         Connection conn1 = null ; 
 5         PreparedStatement ppst1 = null ;
 6         try {
 7             conn = getConn();
 8             conn.setAutoCommit(false);
 9             conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
10             //数据更新前查询
11             String select = "select * from testClass where id = 2 ";
12             ResultSet result1 = conn.prepareStatement(select).executeQuery();
13             //打印查询数据
14             while(result1.next()){
15                 System.out.println("conn(REPEATABLE) before update" + "\n" + 
16                         "count = " + result1.getInt(2));
17             }
18             //数据更新
19             conn1 = getConn();
20             String sql1 = "update testClass set name = 'hello' where id = 2";
21             ppst1 = conn1.prepareStatement(sql1);
22             ppst1.execute(sql1);
23             //数据更新后查询
24             ResultSet result2 = conn.prepareStatement(select).executeQuery();
25             while(result2.next()){
26                 System.out.println("conn(REPEATABLE) after update" + "\n" + 
27                         "count = " + result2.getInt(2));
28             }
29             conn.commit();
30         } catch (SQLException e) {
31             e.printStackTrace();
32         } finally {
33             try {
34                 if (conn != null){
35                     conn.close();
36                 }
37                 if (ppst != null){
38                     ppst.close();
39                 }
40             } catch (SQLException e) {
41                 e.printStackTrace();
42             }
43         }
44     }

 

疯狂摇头的青蛙的主页 疯狂摇头的青蛙 | 初学一级 | 园豆:174
提问于:2017-04-29 20:12
< >
分享
最佳答案
0

TRANSACTION_REPEATABLE_READ:在一个事务中进行查询时,不允许读取其他事务update的数据,允许读取到其他事务提交的新增数据。

根据你的需求,你应该用TRANSACTION_READ_COMMITTED

收获园豆:20
狼爷 | 小虾三级 |园豆:1204 | 2017-05-02 09:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册