异常块中的事务-怎么做? [英] Transaction in an exception block - how?

查看:106
本文介绍了异常块中的事务-怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请一个善良的灵魂指出这是错的地方吗?
我正在尝试运行一个非常简单的事务测试,但是甚至无法运行该语句。我正在尝试在PG管理员的 SQL查询窗口中运行它。没有其他连接到数据库。

  DO $$ 
开始

START交易;

UPDATE IsolationTests SET Col1 = 2;
执行pg_sleep(5.0);

回滚;

除法时,然后
开始
结束;

END $$;

我想了解交易级别并在代码中开始和结束交易。如果我注释掉开始交易和回滚语句运行良好。取消注释,我得到


 错误:无法在PL / pgSQL $ b中开始/结束事务$ b提示:改为使用带有EXCEPTION子句的BEGIN块。 


嗯?


解决方案

回答您的问题-指出这是哪里吗?

解决方案

语句是错误的: START TRANSACTION; 而不是 EXCEPTION BEGIN 语句$ c>阻止...



我不确定您要做什么。这是回滚事务的示例(已创建表t,但未提交表=>不存在),但已捕获并处理了异常(/ 0)(引发信息):

  t =#开始; 
开始
时间:0.124毫秒
t =#
t =#DO $$
t $#BEGIN
t $#开始
t $#创建表t(i int);
t $#结尾;
t $#执行1/0;
t $#例外时,divine_by_zero然后
t $#开始
t $#提高信息%,被EXC抓住;
t $#END;
t $#
t $#END;
t $#$$
t-#;
INFO:捕获到的
DO
时间:10.288 ms
t =#
t =#回滚;
ROLLBACK
时间:0.152 ms
t =#
t =#\dt + t
未找到匹配关系。
t =#;
时间:0.062毫秒


Could some kindly soul point out where this is statement is wrong, please? I'm trying to run a very simple transaction test, but can't even get the statement to run. I'm trying to run it in the SQL Query window of PG Admin. There are no other connections to the database.

DO $$
BEGIN

START TRANSACTION;

UPDATE IsolationTests SET Col1 = 2;
perform pg_sleep(5.0);

ROLLBACK;

EXCEPTION WHEN division_by_zero THEN
BEGIN
END;

END $$;

I want to play around with the transaction level and start and end a transaction in the code. If I comment out the "START TRANSACTION" & "ROLLBACK" the statment runs fine. Uncomment them and I get

"ERROR:  cannot begin/end transactions in PL/pgSQL
HINT:  Use a BEGIN block with an EXCEPTION clause instead."

Uh? It's in a BEGIN ... EXCEPTION block, isn't it??

解决方案

Answering your question - pointing out where this is statement is wrong: START TRANSACTION; and not the BEGIN statement in EXCEPTION block...

I'm not sure what you try to do. Here is example of rolled back transaction (table t created, but not committed => not exists), yet exception (/0) caught and processed (raised info):

t=# begin;
BEGIN
Time: 0.124 ms
t=#
t=# DO $$
t$# BEGIN
t$#   begin
t$#     create table t (i int);
t$#   end;
t$#     perform 1/0;
t$# EXCEPTION WHEN division_by_zero THEN
t$#   BEGIN
t$#     raise info '%','exc catched';
t$#   END;
t$#
t$# END;
t$# $$
t-# ;
INFO:  exc catched
DO
Time: 10.288 ms
t=#
t=# rollback;
ROLLBACK
Time: 0.152 ms
t=#
t=# \dt+ t
No matching relations found.
t=# ;
Time: 0.062 ms

这篇关于异常块中的事务-怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆