时间:2021-07-01 10:21:17 帮助过:27人阅读
若此时 Connection 没有被关闭, 则需要恢复其自动提交状态
常用的代码结构
public void test(String sql, Object... args)
{
Connection conn = null;
PreparedStatement prepareStatement = null;
try
{
conn = getConn();
// 事物处理前,取消Connection的默认提交行为
conn.setAutoCommit(false);
prepareStatement = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++)
{
prepareStatement.setObject(i + 1, args[i]);
}
prepareStatement.executeUpdate();
// 事物处理:如果事物处理成功则提交事物
conn.commit();
}
catch (Exception e)
{
e.printStackTrace();
try
{
// 事物处理:如果出现异常 则在catch块中回滚事物
conn.rollback();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
finally
{ // 释放数据库资源
releaseSource(prepareStatement, conn, null);
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
JDBC ORACLE 事物处理
标签:oracle sql jdbc 事务 java