问题描述
我在项目中使用pg_connect和pg_query。
但是我真的不确定是否使用AutoCommit模式进行pg_connect吗?
I use pg_connect, and pg_query in a project.But I'm really not sure that is pg_connect using AutoCommit mode or not?
这是一个重要的问题,因为我需要在事务下编写一些块,如果服务器将忽略其中一个语句,则数据库将不一致...
It is important question, because I need to write some block under transaction, and if one of the statements would be ignored by the server, the database would be inconsistent...
还有一个有趣的问题,在执行后pg_query会提交吗?
Also interesting question that do pg_query a commit after execution?
例如:
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');
在自动提交模式下工作,那么开始和提交仍然有效吗?
working in AutoCommit mode, so begin and commit is nevertheless?
感谢您的帮助:
dd
Thanks for your help: dd
推荐答案
首先,PostgreSQL中没有AutoCommit模式和PHP API的pg_ *函数不会尝试模拟一个。
First, there is no AutoCommit mode in PostgreSQL and the pg_* functions of the PHP API do not try to emulate one.
pg_query的说
pg_query's doc says
pg_query( UPDATE1 ..; UPDATE2 ...)
在一个事务中执行,并且对数据产生全有或全无的影响。
So it guarantees that pg_query("UPDATE1 ..; UPDATE2...")
executes in one transaction and has an all-or-nothing effect on the data.
序列
pg_query("BEGIN");
pg_query("UPDATE1...");
pg_query("UPDATE2..");
pg_query("COMMIT");
等效于 pg_query( UPDATE1 ..; UPDATE2 ... )
关于数据完整性(半成品状态不会发生)。
is equivalent to pg_query("UPDATE1 ..; UPDATE2...")
with regard to data integrity (half-finished state cannot happen).
关于注释除非有明确的BEGIN / COMMIT。 ..,仅当它们不在整个SQL语句链的开头和结尾时才有意义。
即 pg_query( BEGIN; update1; update2; COMMIT;);
等效于 pg_query( update1; update2; )
,但(显然)不等同于 pg_query( update1; COMMIT; update2;)
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;")
这篇关于PHP:PGSQL驱动程序和AutoCommit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!