zoukankan      html  css  js  c++  java
  • 事务处理

    事务:

    具备一致性、原子性、隔离性

    为了多条语句绑定生效,所以设置默认不提交,执行结束后统一提交

    只要在提交前出现异常,则出现异常之前执行过的语句也不会产生数据持久化,因为数据还没有提交

    在过程中产生异常,需要将数据返回值操作之前,需要设置一个回滚点,但不是必须的,然后在出现异常时,启动回滚

    设置回滚点:

      Savepoint point = con.setSavepoint();

    启动回滚:

     //回滚事务:一般都是不需要设置回滚点的,因为会让其直接回到执行前的原点
     con.rollback();
     //根据设置的回滚点位置,进行事务回滚
     con.rollback(point);
    /**
     * 事务处理
     * 事务具备原子性和持久化的特性
     *
     * 能够将多条增删改语句作为一个整体一起提交,完成持久化
     * 如果其中有任意一条执行失败,整个事务都将会回滚
     */
    public class Demo9 {
    
        public static void main(String[] args)throws Exception {
    
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/geekhome", "root", "tjhilu");
            //将事务的自动提交方法更改为手动提交
            con.setAutoCommit(false);
            //设置回滚点
            Savepoint point = con.setSavepoint();
    
            PreparedStatement pstmt = con.prepareStatement("update account set balance=balance-200 where cardid=1");
            pstmt.executeUpdate();
    
            if(true){
                //回滚事务
                //con.rollback();
                //根据设置的回滚点位置,进行事务回滚
                con.rollback(point);
                throw new RuntimeException("转账失败");
            }
    PreparedStatement pstmt2
    = con.prepareStatement("update account set balance=balance+200 where cardid=2"); pstmt2.executeUpdate(); //提交事务:为了多条语句绑定生效,所以设置默认不提交,执行结束后统一提交 con.commit(); pstmt.close(); con.close(); } }
  • 相关阅读:
    IIS5布署MVC3
    操作数数据类型 ntext 对于 max 运算符无效
    iis配置
    IIS取消目录浏览
    mediamind SyncAds
    告诉你如何做网站运营
    xml as3解析
    mediamind 组件
    AIR for androd 笔记
    开发AIR for android 大象游戏合集开发总结
  • 原文地址:https://www.cnblogs.com/gfl-1112/p/12776271.html
Copyright © 2011-2022 走看看