PhP PDO使查询忽略beginTransaction-commit bloc中的事务

时间:2016-10-25 16:24:28

标签: php mysql pdo transactions

我在循环中有一个查询:

<?php
    try 
    {
        // I ommit the instanciation step of those variables
        $dsn, $user, $pass, $p1, $2, $pdo, $query1, $query2;

        $pdo = new PDO($dsn, $user, $pass);
        $p1 = $pdo->prepare($query1);
        $p2 = $pdo->prepare($query2);

        $pdo->beginTransaction();

        for($i = 0; $i < 99; $i++)
        {
            if( ! $p1->execute() ) 
            {
                $pdo->rollBack();
                break;
            }

            // I want to prevent THIS query to be part of the transaction
            $p2->execute();
        }

        $pdo->commit();
    }
    catch(PDOException $e)
    {
        die($e->getMessage());
    }
?>

问题

我希望$p2->execute();不参与交易。我该怎么做?是否有任何命令使此查询忽略其内部事务集合?

可能的替代

我已经使用了两个PDO实例,如下所示:

<?php
    try 
    {
        // I ommit the instanciation step of those variables
        $dsn, $user, $pass, $p1, $p2, $pdo, $pdo2 $query1, $query2;

        $pdo = new PDO($dsn, $user, $pass);
        $pdo2 = new PDO($dsn, $user, $pass);
        $p1 = $pdo->prepare($query1);
        $p2 = $pdo2->prepare($query2);

        $pdo->beginTransaction();

        for($i = 0; $i < 99; $i++)
        {
            if( ! $p1->execute() ) 
            {
                $pdo->rollBack();
                break;
            }

            // This actually skips the transaction of the first connection
            $p2->execute();
        }

        $pdo->commit();
    }
    catch(PDOException $e)
    {
        die($e->getMessage());
    }
?>

0 个答案:

没有答案