简体   繁体   English

PHP:PGSQL驱动程序和AutoCommit?

[英]PHP: PGSQL driver and AutoCommit?

I use pg_connect, and pg_query in a project. 我在项目中使用pg_connect和pg_query。 But I'm really not sure that is pg_connect using AutoCommit mode or not? 但我真的不确定是否使用AutoCommit模式的pg_connect?

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... 这是一个重要的问题,因为我需要在事务下编写一些块,如果服务器忽略其中一个语句,那么数据库就会不一致......

Also interesting question that do pg_query a commit after execution? 还有一个有趣的问题,pg_query执行后提交?

For example: 例如:

pg_query('begin; update table1...; update table2...; commit');

is same as 和...一样

pg_query('begin;');
pg_query('update table1...;');
pg_query('update table2...;');
pg_query('commit');

and is the 而且是

pg_query('begin; update table1...; update table2...; commit');

working in AutoCommit mode, so begin and commit is nevertheless? 在AutoCommit模式下工作,所以开始和提交仍然是?

Thanks for your help: dd 谢谢你的帮助:dd

First, there is no AutoCommit mode in PostgreSQL and the pg_* functions of the PHP API do not try to emulate one. 首先,PostgreSQL中没有AutoCommit模式,PHP API的pg_ *函数不会尝试模拟一个。

pg_query's doc says pg_query的文档

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 当多个语句传递给函数时,它们将自动作为一个事务执行,除非查询字符串中包含显式的BEGIN / COMMIT命令

So it guarantees that pg_query("UPDATE1 ..; UPDATE2...") executes in one transaction and has an all-or-nothing effect on the data. 因此,它保证pg_query("UPDATE1 ..; UPDATE2...")在一个事务中执行,并对数据产生全或无影响。

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). 就数据完整性而言,相当于pg_query("UPDATE1 ..; 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. 至于注释“除非有明确的BEGIN / COMMIT ......”,只有当它们不在整个SQL语句链的开头和结尾时才有意义。 That is, pg_query("BEGIN; update1; update2; COMMIT;"); 也就是说, pg_query("BEGIN; update1; update2; COMMIT;"); is equivalent to pg_query("update1; update2;") but (obviously) not equivalent to pg_query("update1; COMMIT; update2;") 等效于pg_query("update1; update2;")但(显然)不等同于pg_query("update1; COMMIT; update2;")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM