Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
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

pgsql-novice by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group