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

    /*
            *   事物处理线性操作。
            *    以转账为例
            */
            header('Content-type:text/html;charset=utf-8');
    
            $opt = array(PDO::ATTR_PERSISTENT => TRUE);
            $dsn = "mysql:dbname=0328;host=127.0.0.1";
            $user = "root";
            $password = '';
    
            $dbh = new PDO($dsn,$user,$password,$opt);
            $dbh->setAttribute(PDO::ATTR_AUTOCOMMIT,0); //关闭自动提交
            try{
                $dbh->beginTransaction();
                $price = 10;
                $is_acheived = $dbh->exec("update account set money=money-$price where name='王贺军'");
                if($is_acheived > 0){
                    echo '王贺军成功转出 ' . $price . '元<br>';
                }else{
                    throw new PDOException("转出失败!<br>");
                }
                $is_acheived = $dbh->exec("update account set money=money+$price where name='王二'");
                if($is_acheived > 0){
                    echo '王二成功转入 ' . $price . '元<br>';
                }else{
                    throw new PDOException("转入失败!<br>");
                }
                echo '交易成功!';
                $dbh->commit();
            }catch(PDOException $e){
                echo "交易失败 " . $e->getMessage();
                $dbh->rollback();
            }
            $dbh->setAttribute(PDO::ATTR_AUTOCOMMIT,1); //开启自动提交
            
  • 相关阅读:
    haproxy的使用
    zookeeper 的多线程和单线程库使用对比
    zookeeper 简介
    将博客搬至CSDN
    Sublime Text 添加eclipse快捷键
    Atom编辑器添加eclipse快捷键
    Linux安装mysql教程
    设计模式
    设计模式
    设计模式
  • 原文地址:https://www.cnblogs.com/hejun695/p/5330008.html
Copyright © 2011-2022 走看看