我在项目中使用 pg_connect 和 pg_query。 但是我真的不确定 pg_connect 是否使用 AutoCommit 模式?
这个问题很重要,因为我需要在事务下写一些 block ,如果其中一条语句被服务器忽略,数据库就会不一致...
执行后执行 pg_query 提交也是一个有趣的问题吗?
例如:
pg_query('begin; update table1...; update table2...; commit');
与
相同pg_query('begin;');
pg_query('update table1...;');
pg_query('update table2...;');
pg_query('commit');
是
pg_query('begin; update table1...; update table2...; commit');
在自动提交模式下工作,所以开始和提交仍然是?
感谢您的帮助: dd
最佳答案
首先,PostgreSQL 中没有 AutoCommit 模式,PHP API 的 pg_* 函数也没有尝试模拟这种模式。
pg_query 的 doc说
When multiple statements are passed to the function, they are automatically executed as one transaction, unless there are explicit BEGIN/COMMIT commands included in the query string
因此它保证 pg_query("UPDATE1 ..; UPDATE2...")
在一个事务中执行并对数据产生全有或全无的影响。
顺序
pg_query("BEGIN");
pg_query("UPDATE1...");
pg_query("UPDATE2..");
pg_query("COMMIT");
在数据完整性方面等同于pg_query("UPDATE1 ..; UPDATE2...")
(不会出现半完成状态)。
至于注释“除非有明确的 BEGIN/COMMIT...”,只有当它们不在整个 SQL 语句链的开头和结尾时才有意义。
也就是说,pg_query("BEGIN; update1; update2; COMMIT;");
等同于 pg_query("update1; update2;")
但(显然)不等同于pg_query("update1; COMMIT; update2;")
关于PHP:PGSQL 驱动程序和 AutoCommit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9704557/