请教各位高手,小弟想用事务来同时更新两个表,目的是两个表同时得到更新,或者失败时同时回滚,
我在测试的时候注释掉了一条语句使得程序在更新第二个表时抛出异常,并导致回滚,
可是不知道哪里的原因,对第一个表的更新居然提交了,只有第二个表发生了会滚,
我在debug 的时候也发现对第一个表更新时只要 pstmt.executeUpdate(); 执行,数据库里的内容就已经更改了,这是为什么阿 ?
还望各位指点和指正!使用的数据库是 mysql.多谢了! try {
               conn.setAutoCommit(false);
                               
                String sql="insert into ticket_change values (?,?,?,?,?,?)";
                pstmt=conn.prepareStatement(sql);
                
                /*
                 *update the table 'ticket_change'
                 *
                 */
                for(Map.Entry<String,String> e : newProperties.entrySet()){
                    
                    pstmt.setInt(1,id);
                    pstmt.setInt(2,changeTime);
                    pstmt.setString(3,"user");
                    pstmt.setString(4,e.getKey());
                    pstmt.setString(5,oldProperties.get(e.getKey()));
                    pstmt.setString(6,e.getValue());
                    
                    pstmt.executeUpdate();
                }
                
                System.out.println("OK!!!!!!!!!!!");
                
                
                /*
                 *update the table 'ticket'
                 *
                 */
                sql="update ticket set " +
                        "summary = ? ,type = ? ,priority = ? ,milestone = ? ," +
                        "component = ? ,version = ?, keywords = ?, cc = ? ," +
                        "changetime = ? " +
                        "where id=" + id;
                pstmt=conn.prepareStatement(sql);
                
                pstmt.setString(1,newProperties.get("summary"));
// 注释掉这行使产生异常
//                pstmt.setString(2,newProperties.get("type"));
                pstmt.setString(3,newProperties.get("priority"));
                pstmt.setString(4,newProperties.get("milestone"));
                pstmt.setString(5,newProperties.get("component"));
                pstmt.setString(6,newProperties.get("version"));
                pstmt.setString(7,newProperties.get("keywords"));
                pstmt.setString(8,newProperties.get("cc"));
                pstmt.setInt(9,changeTime);
                pstmt.executeUpdate();
                
                conn.commit();
//                conn.setAutoCommit(true);
                
            } catch (SQLException ex) {
                
                System.out.println("error by changing the ticket");
                try {
                    conn.rollback();
                    
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                
            }

解决方案 »

  1.   

    1。我估计是事务开始 conn.setAutoCommit(false);
    用的不对,
    2。刷新一下数据库看看是不是真的提交了
      

  2.   

    /*
                     *update the table 'ticket'
                     *
                     */
                    sql="update ticket set " +
                            "summary = ? ,type = ? ,priority = ? ,milestone = ? ," +
                            "component = ? ,version = ?, keywords = ?, cc = ? ," +
                            "changetime = ? " +
                            "where id=" + id;
                    pstmt=conn.prepareStatement(sql);
                    
                    pstmt.setString(1,newProperties.get("summary"));
    // 注释掉这行使产生异常
    //                pstmt.setString(2,newProperties.get("type"));
                    pstmt.setString(3,newProperties.get("priority"));
                    pstmt.setString(4,newProperties.get("milestone"));
                    pstmt.setString(5,newProperties.get("component"));
                    pstmt.setString(6,newProperties.get("version"));
                    pstmt.setString(7,newProperties.get("keywords"));
                    pstmt.setString(8,newProperties.get("cc"));
                    pstmt.setInt(9,changeTime);
                    pstmt.executeUpdate();
                    
                    conn.commit();
    ----------------------------------------------------------------------------
    楼主把pstmt换成一个重新声明的pstmt2试试。
      

  3.   

    不要用两个名字都是pstmt试试
      

  4.   

    conn.setAutoCommit(false)
    要放在try块外面
    而conn.setAutoCommit(false)
    应该放在finally块里象你这样try快执行的时候conn还是处于自动提交状态
      

  5.   

    其实代码没什么问题,只是我用得mysql 数据库引擎不支持事务处理,暴汗!
    不过还是要谢谢楼上几位!