简体   繁体   English

PostgreSQL事务:检查查询错误

[英]postgresql transaction: check for query error

i'm writing a postgresql function implementing a transaction. 我正在编写实现事务的postgresql函数。 this how the code looks like: 代码如下所示:

BEGIN TRANSACTION REPEATABLE READ
INSERT INTO table1 VALUES(value1, value2);
INSERT INTO table2 VALUES(value3);
COMMIT
END;

Now i need to check if one of the insert query return some kind of error for insert rollback. 现在,我需要检查插入查询之一是否为插入回滚返回某种错误。 How can i do it? 我该怎么做?

Checking for, and handling errors, is good. 检查并处理错误是好的。 Do it consistently. 始终如一。 While transactions will automatically roll back, you may want to alert the user log the problem, or take other actions. 尽管事务将自动回滚,但您可能想提醒用户记录问题或采取其他措施。 This being said the transaction will roll back on its own if there is a database error. 这就是说如果有数据库错误,事务将自行回滚。

On the other hand, if you need to roll back a transaction because of an application error (outside the database), you issue a ROLLBACK command. 另一方面,如果由于应用程序错误(数据库外部)而需要回滚事务,则发出ROLLBACK命令。

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

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