【发布时间】:2010-10-26 22:00:25
【问题描述】:
我正在使用 JDBC(使用 mysql)在 java 中测试回滚,但下面的代码不起作用。代码表现得好像我调用了 commit() 而不是 rollback()。谁能告诉我哪里出错了?
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)
{}
}
}
【问题讨论】:
标签: mysql jdbc transactions