PostgreSQL自定义异常

  在Oracle的procedure里,我们会用for update nowait锁一些记录,防止多个用户同时修改同一条记录。为了捕捉ora-00054错误,并对用户进行友好提示,开发人员自定义了一个exception,叫RESOURCE_BUSY_EXCEPTION,关联的Oracle错误代码就是ora-00054。这个自定义的exception在EDB里报错。

  原因是EDB里的自定义异常,只能绑定-20000 and -20999之间的错误代码。

  在PG中,捕获未获得行锁的异常处理的例子:

1.通过错误码捕获

do $$declare
errmsg text;
begin
select * from t1 for update nowait;
exception
when SQLSTATE '55P03' then
raise INFO 'row locked by others';
when others then
raise INFO 'others error';
end$$;

2.通过异常名捕获

do $$declare
errmsg text;
begin
select * from t1 for update nowait;
exception
whenlock_not_available then
raise INFO 'row locked by others';
when others then
raise INFO 'others error';
end$$;

猜你喜欢

转载自bijian1013.iteye.com/blog/2242955