473,432 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,432 software developers and data experts.

Problem with a Postgresql's procedure to deal with exception in transaction

2 2Bits
Hello,

I'm trying to control a transaction with a procedure but I'm getting an error I cannot resolve.

This is the code:

SQL

drop table if exists cuentas;

create table cuentas (
id serial,
nombre varchar(100) not null,
saldo dec(15,2) not null,
primary key(id),
constraint saldo CHECK(saldo > 5000)
);


insert into cuentas(nombre, saldo)
values('Carlos',10000);

insert into cuentas(nombre, saldo)
values('Cesar',10000);

SELECT * FROM cuentas;

drop procedure if exists transferir;

create or replace procedure transferir(id_emisor int,id_receptor int,cantidad dec)
language plpgsql
as $$
begin
-- adding the amount from the recievers's account
update cuentas
set saldo = saldo + cantidad
where id = id_receptor;
-- substracting the amount to the sender's account
update cuentas
set saldo = saldo - cantidad
where id = id_emisor;
commit;
EXCEPTION
WHEN check_violation THEN
RAISE NOTICE 'Violación de saldo.';
rollback;
end;$$


When I try to execute:
SQL

call transferir(1,2,1000);

I get:
SQL

ERROR: no se puede comprometer mientras hay una subtransacción activa
CONTEXT: función PL/pgSQL transferir(integer,integer,numeric) en la línea 13 en COMMIT
Estado SQL: 2D000

Can anybody see what I'm doing wrong?

Thank you

Have a nice day !
Feb 26 '21 #1
2 3693
Niheel
2,460 Expert Mod 2GB
Try moving the commit; outside of the transaction block.

In the block, as you have it:
Expand|Select|Wrap|Line Numbers
  1. begin
  2. -- adding the amount from the recievers's account
  3. update cuentas
  4. set saldo = saldo + cantidad
  5. where id = id_receptor;
  6. -- substracting the amount to the sender's account
  7. update cuentas
  8. set saldo = saldo - cantidad
  9. where id = id_emisor;
  10. commit;
  11. EXCEPTION
  12. WHEN check_violation THEN
  13. RAISE NOTICE 'Violación de saldo.';
  14. rollback;
  15. end;$$
More information here:
https://www.postgresql.org/docs/11/p...nsactions.html
A transaction cannot be ended inside a block with exception handlers.
Feb 26 '21 #2
Gabrach
2 2Bits
@Niheel
I appreciate your comment "A transaction cannot be ended inside a block with exception handlers."

If I've understood well that means that if I use an exception handler I will not be able to finish the transaction.

It surprises me a lot that Postgresql is not capable to work with an error in the transaction so you can rollback. Other languages allow you to catch the error and do a rollback.
It should be a way.

Thank you
Feb 28 '21 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Krzysztof Rozmus | last post by:
Hi, I have stored procedure (MS SQL Server 2000) which operates on around 600 000 rows (SELECT, UPDATE, INSERT) and executes in 5 minutes, when I put it in SQL transaction it slows down to...
3
by: war_wheelan | last post by:
I am having a problem backing up my database and TLog files due to a lack of local diskspace. The db file is about 30GB and the TLog is about 20GB each on a different hard disk. Each disk doesn't...
7
by: Dirk Deimeke | last post by:
Hi! We have two (nearly) identical systems, one is a production system, the second is a quality system. Becaus of any events, I get an error in the quality system: SQL0444N Routine...
1
by: Filips Benoit | last post by:
Hi, Can I mix ADO-code (insert, update recordsets) and stored procedures in a transaction? Filip
8
by: Don Riesbeck Jr. | last post by:
I have an C# EnterpriseService component that is part of an application I am developing that is responsible for writing data to an SQL Server. (it reads from a local DB (MSDE), then writes to a...
5
by: Karl | last post by:
Hi, I have some code that will save the contents of a Rich Text Box in either a Text or Rich Text Format file. The code is using the SaveFileDialog and is working correctly. I have been...
0
by: mbench | last post by:
I am having problems with the following procedure call, does anyone know what I am doing wrong? Below the procedure is the error message I receive. -- Stored procedure to do a non-clustered index...
2
by: meendar | last post by:
Hi, I have selected a field name and declared it as varchar, since it is varchar in table and performed some numeric operation with numbers, even after i cast the sql in below code, it throws...
1
by: hussain123 | last post by:
Hi All, I am invoking a procedure which takes 2 IN parameters and both are dates which are passed as string.In the procedure I am using those 2 IN parameters to query the db and fetch record between...
2
by: ToddJ | last post by:
I have written a stored procedure to query for cities which start with the same first few letters and state. When I run the stored procedure, it only seems to use the first character of each...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.