时间:2021-07-01 10:21:17 帮助过:5人阅读
有时候一个事务可能是一组复杂的语句,因此可能想要回滚到事务中某个特殊的点。JDBC Savepoint帮我们在事务中创建检查点(checkpoint),这样就可以回滚到指定点。当事务提交或者整个事务回滚后,为事务产生的任何保存点都会自动释放并变为无效。把事务回滚到一个保存点,会使其他所有保存点自动释放并变为无效。
Savepoint savepoint = null; try { con = DBConnection.getConnection(); // set auto commit to false con.setAutoCommit(false); // do Business
// if code reached here, means main work is done successfully savepoint = con.setSavepoint("SavePoint1"); insertLogData(con, 2); // now commit transaction con.commit(); } catch (SQLException e) { e.printStackTrace(); try { if (savepoint == null) { // SQLException occurred in saving into Employee or Address // tables con.rollback(); System.out.println("JDBC Transaction rolled back successfully"); } else { // exception occurred in inserting into Logs table // we can ignore it by rollback to the savepoint con.rollback(savepoint); // lets commit now con.commit(); } } catch (SQLException e1) { System.out.println("SQLException in rollback" + e.getMessage()); } }
内容来自http://www.importnew.com/8832.html。
JDBC事务管理及SavePoint示例
标签:imp trace else 服务器 资源 mit led nec 完成