How should I end a transaction with possibly failed queries?

From: Sergey Samokhin <prikrutil(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: How should I end a transaction with possibly failed queries?
Date: 2009-09-02 19:18:58
Message-ID: e42595410909021218x1c93f8e2ic59b69f5b51f056c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hello.

Does it seem right to send "COMMIT" without checking whether any of
the queries I've done in the transaction failed?

Here is pseudo code illustrating this behaviour where "COMMIT" is
always sent no matter what result of do_some_queries() was:

some_transaction(Conn)
{
query(Conn, "BEGIN"),
try do_some_queries(Conn)
catch
Reason ->
Reason
after
query(Conn, "COMMIT")
end
}

Is this correct? What if there is a select from non-existing table
inside do_some_queries()? Should I detect it (e.g. by thrown an
exception) and do ROLLBACK as the following code does:

some_transaction(Conn)
{
try query(Conn, "BEGIN"),
do_some_queries(Conn),
query(Conn, "COMMIT")
catch
Reason ->
query(Conn, "ROLLBACK"),
Reason
end
}

First sample seems more elegant, but I'm not sure that it's safe. Will
there be any problems with this in the future?

Thanks.

--
Sergey Samokhin

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Ruzsinszky Attila 2009-09-02 19:38:44 Re: Modifying selected records
Previous Message Ruzsinszky Attila 2009-09-02 18:18:07 Re: Modifying selected records