How should I end a transaction with possibly failed queries? - Mailing list pgsql-novice

From Sergey Samokhin
Subject How should I end a transaction with possibly failed queries?
Date
Msg-id e42595410909021218x1c93f8e2ic59b69f5b51f056c@mail.gmail.com
Whole thread Raw
Responses Re: How should I end a transaction with possibly failed queries?
List 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

pgsql-novice by date:

Previous
From: Ruzsinszky Attila
Date:
Subject: Re: Modifying selected records
Next
From: Ruzsinszky Attila
Date:
Subject: Re: Modifying selected records