开发者

jdbc rollback not working

I'm testing rollback in java using JDBC (with mysql), but the code below is not working. The code is behaving as if I called commit() instead of rollback(). Can somebody tell me where I'm going wrong?

        Connection conn=null;
    try
    {
    conn = SqlConnectionHandler.getSqlConnection();
    conn.setAutoCommit(false);
    }
    catch(Exception e)
    {
        System.exit(-1);
    }
    String updateString1 = "update files set ownerID=ownerID+1 where globalFileID=2";
    String updateString2 = "update directories set size=size+1 where globalDirID=8";

    try
    {
        Statement statement = conn.createStatement();
        statement.executeUpdate(updateString1);
        statement.executeUpdate(updateString2);
        conn.rollback();
       开发者_运维问答 //conn.commit();
        statement.close();
        SqlConnectionHandler.closeConnection(conn);
    }
    catch(Exception ex)
    {
        System.err.println("SQLException: " + ex.getMessage());
        try
        {
        conn.rollback();
        }
        catch(SQLException e)
        {}
    }
}


Found the problem here and here. Seems I was using the default storage engine myISAM, which does not support transactions. Changing the storage engine to InnoDB reportedly works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜