Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mayb8-0001SJ-2X for psycopg@arkaria.postgresql.org; Thu, 14 Oct 2021 11:08:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mayb7-0005Do-1v for psycopg@arkaria.postgresql.org; Thu, 14 Oct 2021 11:08:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mayb6-0005De-Re for psycopg@lists.postgresql.org; Thu, 14 Oct 2021 11:08:28 +0000 Received: from mail-ed1-f43.google.com ([209.85.208.43]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mayb4-0002Jo-H9 for psycopg@postgresql.org; Thu, 14 Oct 2021 11:08:27 +0000 Received: by mail-ed1-f43.google.com with SMTP id r18so22556744edv.12 for ; Thu, 14 Oct 2021 04:08:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=EJq6KeKd6W2MsCBjU3+ivTrpeIL3uewXSrQLGscwxp4=; b=xPVa6QN0uzzG5+tSYKRSIMn0EciEXbB7yN/typge8C2JS0a+NqshgoM94MriHbQ+eT 1UVeGCtTN2YuoF56fLBhaXR6EUidgGjUJvVAO+GRKJTeErJjd1CLELJzaMpyaU1rcCsj wAPJnphsCGAYDdxg2adnmeoqQVVlsJNtF+ix62N/QZpAXUhXZxcLX68dLijJPOyaEmgR LtBANbzQLZJFVb6umSlvUgA2uSvPEOYhIDmw/6dCT7f89wYd5gO2HY2HrgWJiJmI/OuI V7FY41BJ3FGxXqjjhywrvkxY5NQJLvvAYQHLpJZ2GQUOSr47zoxtOrXaRWv+LwQ7gC42 Upjw== X-Gm-Message-State: AOAM532icf8oDq6vvH9vzmWdRiBiK7hQLZV/q2Vs0avmRjqggHj43ecb cMIWs70jDAEiYzK+3dOgHPg6Bc5DHh8gotEElUQ= X-Google-Smtp-Source: ABdhPJz7WH/jgRVFjmOJ/qU52FeMrz4DjP53DIM/l3m9PxY6vOSmoOda/HwhNpX3TSJ9jx5Xs57RLREWDV13r8meGwY= X-Received: by 2002:a50:d9c4:: with SMTP id x4mr7927563edj.8.1634209704285; Thu, 14 Oct 2021 04:08:24 -0700 (PDT) MIME-Version: 1.0 References: <9a99d2d110b121542d1ea0a05066858b@paolodestefani.it> In-Reply-To: From: Daniel Fortunov Date: Thu, 14 Oct 2021 12:08:11 +0100 Message-ID: Subject: Re: Re: psycopg3 transactions To: Karsten Hilbert Cc: Daniele Varrazzo , Paolo De Stefani , Psycopg Content-Type: multipart/alternative; boundary="000000000000e9981e05ce4e183d" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000e9981e05ce4e183d Content-Type: text/plain; charset="UTF-8" On Thu, 14 Oct 2021, 11:05 Karsten Hilbert, 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.) --000000000000e9981e05ce4e183d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Thu, 14 Oct 2021, 11:05 Karsten Hilbert, <Karsten.Hilbert@gmx.net> wrote:
>My conclusion is that the only sane th= ing to do is:
>1. Only ever create connections in autocommit mode.
>2. Only ever use `with connection.transaction()` to control transaction= s.
>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 stil= l "using transactions", just with more precise, more explicit*, a= nd more flexible* semantics, represented by a context manager.

Rolling back a transaction is possib= le by raising a Rollback exception within a block.
<= br>
I hope this answers your question but if not ple= ase describe the scenario you are thinking about.
Dani

(*More explicit because=C2=A0= in the conventional autocommit=3Dfalse approach, the beginning of the trans= action is an implicit side-effect of executing another statement, which may= or may not actually begin a transaction depending on whether one is in pro= gress. More flexible because you can have nested transaction blocks which o= perate in an independent and composable way.)


--000000000000e9981e05ce4e183d--