postgreSQL(一) 函数事务

  1. 第一种情况:
    第一个代码块和第二个代码块都没有提交。
    如果整个函数捕获到了异常,即执行到了“总异常”处,则整个函数都不会提交。即使第一个begin end块虽然没有异常,正确执行,但还是不会提交事务。
  2. 第二种情况:
    第一个代码块提交,第二个代码块没提交。
  3. 总结:
    异常在哪个块捕获到,就影响哪个块的begin..end中的事务提交。
-- 测试事务
create or replace function test_tsct()
returns integer 
as $BODY$
declare
	result integer;
begin
	
	begin
	INSERT INTO public.user_info(
	id,name, birthday, address, created_date)
	VALUES (uuid_generate_v4(), 'name_3',to_date('1986-7-7','yyyy-mm-dd') , '成都', now());
	exception
			when others then
			insert into t_log (msg) values('1异常');
	end;		
			
	-- 第一种情况,全部回滚	
	-- result=1/0;
			
	begin
	INSERT INTO public.user_info(
	id,name, birthday, address, created_date)
	VALUES (uuid_generate_v4(), 'name_4',to_date('1986-7-7','yyyy-mm-dd') , '成都', now());
        -- 第二种情况,只是本begin end代码块不提交		
	result=1/0;		
	exception
			when others then
			insert into t_log (msg) values('2异常');
	end;		

	return 1;
	
exception
			when others then
			insert into t_log (msg) values('总异常');
			return 0;
			
end;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;

复制代码
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值