views:

14

answers:

1

Hi, 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)
        {}
    }
}
A: 

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.

apoorv020