设置事务的回滚点

package cn.itcast.jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Savepoint; import cn.itcast.utils.JdbcUtils; public class Demo2 { /** * 事务回滚点 * @throws SQLException */ public static void main(String[] args) throws SQLException { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; Savepoint sp = null; try{ conn = JdbcUtils.getConnection(); conn.setAutoCommit(false); //start String sql1 = "update account set money=money-100 where name='aaa'"; conn.prepareStatement(sql1).executeUpdate(); sp = conn.setSavepoint(); //设置回滚点 String sql2 = "update account set money=money-100 where name='aaa'"; conn.prepareStatement(sql2).executeUpdate(); String sql3 = "update account set money=money+100 where name='bbb'"; conn.prepareStatement(sql3).executeUpdate(); int x = 1/0; conn.commit(); }catch (Exception e) { conn.rollback(sp); //显示通知数据回滚(只回滚sql2,和sql3两条语句,sql1提交) conn.commit(); } finally{ JdbcUtils.release(conn, st, rs); } } }

你可能感兴趣的:(设置事务的回滚点)