官术网_书友最值得收藏!

Handling errors inside a transaction

It is not always the case that transactions are correct from beginning to end. Things might just go wrong for whatever reason. However, in PostgreSQL, only error-free transactions can be committed. The following listing shows a failing transaction, which errors out due to a division-by-zero error:

test=# BEGIN; 
BEGIN
test=# SELECT 1; ?column?

----------
1
(1 row)
test=# SELECT 1 / 0;
psql: ERROR: division by zero
test=# SELECT 1; psql: ERROR: current transaction is aborted, commands ignored until end of transaction block
test=# COMMIT; ROLLBACK

Note that division by zero did not work out.

In any proper database, an instruction similar to this will instantly error out and make the statement fail.

It is important to point out that PostgreSQL will error out, unlike MySQL, which is far less strict. After an error has occurred, no more instructions will be accepted, even if those instructions are semantically and syntactically correct. It is still possible to issue COMMIT. However, PostgreSQL will roll back the transaction because it is the only correct thing to be done at that point.

主站蜘蛛池模板: 贵南县| 呼图壁县| 延寿县| 新田县| 永平县| 蒙山县| 五大连池市| 泸州市| 寿光市| 巴中市| 新民市| 芜湖县| 曲靖市| 铁岭县| 武威市| 吴江市| 得荣县| 吴江市| 中山市| 西丰县| 德江县| 朝阳市| 卢湾区| 抚州市| 亳州市| 金乡县| 东乡| 永吉县| 三门峡市| 当雄县| 黎平县| 红桥区| 民丰县| 尚义县| 崇文区| 怀来县| 泊头市| 抚宁县| 彰化市| 大冶市| 邯郸市|