PHP: PGSQL driver and AutoCommit?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 07:44:18

First, there is no AutoCommit mode in PostgreSQL and the pg_* functions of the PHP API do not try to emulate one.

pg_query's doc says

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

So it guarantees that pg_query("UPDATE1 ..; UPDATE2...") executes in one transaction and has an all-or-nothing effect on the data.

The sequence

pg_query("BEGIN");
pg_query("UPDATE1...");
pg_query("UPDATE2..");
pg_query("COMMIT");

is equivalent to pg_query("UPDATE1 ..; UPDATE2...") with regard to data integrity (half-finished state cannot happen).

As for the note "unless there are explicit BEGIN/COMMIT...", it is relevant only if these are not at the beginning and end of the entire chain of SQL statements. That is, pg_query("BEGIN; update1; update2; COMMIT;"); is equivalent to pg_query("update1; update2;") but (obviously) not equivalent to pg_query("update1; COMMIT; update2;")

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!