加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 为什么以下PDO事务提交?

发布时间:2020-12-13 18:08:02 所属栏目:PHP教程 来源:网络整理
导读:我认为这个问题本身就是不言自明的.代码如下 – ?php $PDO = NULL; $pdo_dsn = 'mysql:host=localhost;dbname=pdo_test'; $pdo_persistence = array( PDO::ATTR_PERSISTENT = true ); $db_user = 'root'; $db_pass = ''; $db_query = "INSERT INTO person(na
我认为这个问题本身就是不言自明的.代码如下 –
<?php
    $PDO = NULL;
    $pdo_dsn = 'mysql:host=localhost;dbname=pdo_test';
    $pdo_persistence = array( PDO::ATTR_PERSISTENT => true );
    $db_user = 'root';
    $db_pass = '';
    $db_query = "INSERT INTO person(name,address)
                    VALUES ('Mamsi Mamsi','Katabon')";

    try
    {
            $PDO = new PDO($pdo_dsn,$db_user,$db_pass,$pdo_persistence);
    }
    catch(PDOException $e)
    {
            echo "Error occured: ". $e->getMessage();
            die();
    }

    $PDO->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $PDO->setAttribute(PDO::ATTR_AUTOCOMMIT,false);

    try
    {
            $PDO->beginTransaction();
            $PDO->exec($db_query);

            throw new PDOException('Generated Exception');

            $PDO->commit();
    }
    catch(PDOException $e)
    {
            echo "An error occured while doing a database transaction. The 
            error message is : ".$e->getMessage();

            $PDO->rollBack();
            die();
    }
?>

即使我在catch块中回滚事务,数据仍然被插入到数据库中.为什么?

编辑

我将在documentation中添加以下几行以进一步澄清 –

Unfortunately,not every database supports transactions,so PDO needs
to run in what is known as “auto-commit” mode when you first open the
connection. Auto-commit mode means that every query that you run has
its own implicit transaction,if the database supports it,or no
transaction if the database doesn’t support transactions. If you need
a transaction,you must use the PDO::beginTransaction() method to
initiate one. If the underlying driver does not support transactions,
a PDOException will be thrown (regardless of your error handling
settings: this is always a serious error condition). Once you are in
a transaction,you may use PDO::commit() or PDO::rollBack() to finish
it,depending on the success of the code you run during the
transaction.

此外,从this页面开始以下行 –

bool PDO::beginTransaction  ( void  )

Turns off autocommit mode. While autocommit mode is turned off,
changes made to the database via the PDO object instance are not
committed until you end the transaction by calling PDO::commit().
Calling PDO::rollBack() will roll back all changes to the database
and return the connection to autocommit mode.

Some databases,including MySQL,automatically issue an implicit
COMMIT when a database definition language (DDL) statement such as
DROP TABLE or CREATE TABLE is issued within a transaction. The
implicit COMMIT will prevent you from rolling back any other changes
within the transaction boundary.

您应该检查您是否使用INNODB作为数据库类型. MyISAM不支持交易.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读