各位:我想试验java数据库事务,在设置connection.setAutoCommit(false)后,为什么不调用commit()也能提交?并且在其中一个更新数据操作有异常时,rollback()也不起作用,我使用的是mysql数据库(5.x).

解决方案 »

  1.   

    没有commit也能提交,不可能吧
      

  2.   

    public void setAutoCommit(boolean flag) {
    try {
    if (conn != null) {
    conn.setAutoCommit(flag);
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    public void commit() {
    try {
    if (conn != null) {
    conn.commit();
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    try{
    // 批处理 start2
    Connection conn = dbm.conn();
    Statement stmt = conn.createStatement();String getListSize = request.getParameter("listSize");
    String[] id = request.getParameterValues("id");
    String[] userName = request.getParameterValues("userName");
    String[] passWord = request.getParameterValues("passWord");
    int listSize = java.lang.Integer.parseInt(getListSize);
    // 事务处理1
    dbm.setAutoCommit(false);
    // 事务处理1结束
    for (int i = 0; i < listSize; i++) {
    String sql = "UPDATE userInfo set userName='" + userName[i]
    + "',passWord='" + passWord[i] + "' where id='" + id[i]
    + "'";
    stmt.addBatch(sql);
    }
    //执行并返回对数据库影响的行数
    int[] counts=stmt.executeBatch();
    // for (int i = 0; i < counts.length; i++) {
    // System.out.println("counts[]=" + counts[i]);
    // }
    // 事务处理2
    dbm.commit();
    dbm.setAutoCommit(true);
    // 事务处理2结束
    // 批处理end2}
      

  3.   

    dbm.commit();
    dbm.setAutoCommit(true);
    注释掉,看是否还能进行提交,把自动提交设置成false是不自动提交事务的
      

  4.   

    mysql是不是那个版本一下的不支持事务?