2012-02-12 69 views
0

我遇到问题。MySQL提交和回滚失败

try {     
    jdbcConnect(); //get mysql connect 
    conn.setAutoCommit(false); 

    pstmt = conn.prepareStatement ( 
        "INSERT INTO member (member_name, member_introduce) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS); 

    pstmt.setString(1, "something"); 
    pstmt.setString(2, "something"); 
    pstmt.executeUpdate(); 
    rs = pstmt.getGeneratedKeys(); 
    rs.next(); 
    String no = Integer.toString(rs.getInt(1); 

    pstmt = conn.prepareStatement ("UPDATE account SET account_name = ? WHERE account_no = ?"); 
    pstmt.setString(1, "something"); 
    pstmt.setString(2, no); 
    pstmt.executeUpdate(); 

    conn.commit();   
    conn.setAutoCommit(true); 

} catch (SQLException t) {    
    try { 
     if (conn != null) { 
      conn.rollback(); 
      conn.setAutoCommit(true); 
     } 
    } catch (SQLException e) { 
    } 
}//close conn and prepareStatement 

我期待着工作提交。

但是,如果更新语句发生错误,插入语句正在运行。

出了什么问题?

回答

4

我认为你使用的是MyISAM表而不是InnoDB。 MyISAM根本不支持事务。

+0

谢谢!! @jdevelop – blim 2012-02-12 12:49:12

+0

现货。这是我的问题。 – Sonny 2017-03-14 18:02:43