【问题标题】:Don't rollback on errors in transactions using PHP, PDO and Postgres不要回滚使用 PHP、PDO 和 Postgres 的事务中的错误
【发布时间】:2012-12-10 04:14:48
【问题描述】:

我有一个非常广泛的基于 PHP/Yii 的 PHP 更新脚本,它可以在不同的数据库类型(MSSQL、Postgres 和 MySQL)上更新数据库。

整个脚本在事务中运行。但是,有些语句会导致查询错误(例如,如果某个键已存在于表中)。我用try/catch 语句包围了这些 - 到目前为止,这在 MySQL 中运行良好

但是在 Postgres 上,一旦发出无效查询,事务就会自动失败。以下所有语句均显示以下错误消息:

CDbCommand failed to execute the SQL statement: SQLSTATE[25P02]: In failed sql transaction: ERROR: current transaction is aborted, commands ignored until end of transaction block

但我需要 Postgres 来继续事务,因为我想在应用程序中决定何时回滚 - 或者以某种方式清除错误并继续事务。

怎么做?

【问题讨论】:

  • 如果脚本检测到“重复键错误”,下一步是什么?
  • @wildplasser:其他一些不相关的声明。

标签: php postgresql pdo yii


【解决方案1】:

为此目的使用保存点:

BEGIN; -- transaction starts here

-- do things if you want

SAVEPOINT my_savepoint;

-- failing statement here
-- all other statements are ignored

ROLLBACK TO SAVEPOINT my_savepoint;

-- continue your transaction starting from my_savepoint

COMMIT;

【讨论】:

    猜你喜欢
    • 2015-07-14
    • 2011-01-26
    • 2013-05-13
    • 2012-09-12
    • 2011-01-10
    • 2014-08-23
    • 1970-01-01
    • 2014-03-17
    • 2014-05-09
    相关资源
    最近更新 更多