Re: Re: psycopg3 transactions

From: Daniel Fortunov <postgresql(at)danielfortunov(dot)com>
To: Karsten Hilbert <Karsten(dot)Hilbert(at)gmx(dot)net>
Cc: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>, Paolo De Stefani <paolo(at)paolodestefani(dot)it>, Psycopg <psycopg(at)postgresql(dot)org>
Subject: Re: Re: psycopg3 transactions
Date: 2021-10-14 11:08:11
Message-ID: CAH1rg6av5MR+yhEpwaxZNH+QrW+9ya9BQTx+w_Q9crm3e65yZQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On Thu, 14 Oct 2021, 11:05 Karsten Hilbert, <Karsten(dot)Hilbert(at)gmx(dot)net> wrote:

> >My conclusion is that the only sane thing to do is:
> >1. Only ever create connections in autocommit mode.
> >2. Only ever use `with connection.transaction()` to control transactions.
> >3. Forget that `connection.commit()` and `connection.rollback()` exist,
> and never use them.
>
> What if you need to rollback a hitherto valid transaction ?
>
> Transactions are there for a reason. It seems best to explicitely use them
> ?
>

I'm not sure I fully understand your question/statement but let me try to
respond.

We are still "using transactions", just with more precise, more explicit*,
and more flexible* semantics, represented by a context manager.

Rolling back a transaction is possible by raising a Rollback exception
within a block.

I hope this answers your question but if not please describe the scenario
you are thinking about.

Dani

(*More explicit because in the conventional autocommit=false approach, the
beginning of the transaction is an implicit side-effect of executing
another statement, which may or may not actually begin a transaction
depending on whether one is in progress. More flexible because you can have
nested transaction blocks which operate in an independent and composable
way.)

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Karsten Hilbert 2021-10-14 22:16:25 Re: Re: psycopg3 transactions
Previous Message Karsten Hilbert 2021-10-14 10:05:03 Aw: Re: psycopg3 transactions