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 1ma0aw-0007UG-Nj for psycopg@arkaria.postgresql.org; Mon, 11 Oct 2021 19:04:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ma0au-0001bH-T5 for psycopg@arkaria.postgresql.org; Mon, 11 Oct 2021 19:04:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ma0au-0001b9-LY for psycopg@lists.postgresql.org; Mon, 11 Oct 2021 19:04:16 +0000 Received: from mail-ua1-x930.google.com ([2607:f8b0:4864:20::930]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ma0ar-0005Td-Ph for psycopg@postgresql.org; Mon, 11 Oct 2021 19:04:15 +0000 Received: by mail-ua1-x930.google.com with SMTP id f4so8938244uad.4 for ; Mon, 11 Oct 2021 12:04:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=X1B7kPAyt9Mkl1vhNOwslXCa5l13CYg6uHeo/yzO5AM=; b=dCAZL2R4dKMNmK4Swsu8uZhpZHpLGAyYo4ZTKhOzRk7oqAIsa2IfZMfBr8oI0omQiH 2zWqvFSw6cgcBW+UJEIk4OVZy5s3rj+MqKhVPeeAjqMJXZf8EuY7AwauJrqj1bAX0dsD irpVbbJut8kFpGiROIILI6Qk7fUXV3wJ2/gKp6dL6fXRT2Ulw40LVbTboKB2TpIdACA6 F47abxpH3hiG0t6tFgnPImm6WsX/4/UvKWSuWLBfwUkkLeg0GWwOiNkN4luJtZtYqzdR DZEsYf/KFyBDqKTd5M2/533dJmFz9CavFJSAkVfsqUhzEsq9Kqld/uDsZoL+3GVbJVE5 nujA== 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=X1B7kPAyt9Mkl1vhNOwslXCa5l13CYg6uHeo/yzO5AM=; b=L13otTHJQbWBhbuKv/GEgF60qu3Tw8szjQCGN82THOCownvhMu0VN0S8GKujU9gv11 +n6JCF1ZoPPA6rIBp/MbBzLTPhh0R//qNeXeh1/SuwEvq9b5Wn3NMLuhxZDkbrisCq5M Uhvu8OTsm3uA2Mnwb+7nT+9QWUtTxWNX5Io24+jCwaByOF1deq/m/n7mD4snHbpR2r2h nR87xlLqWDEWB0S3+mHxIccAZYHJcyLHQFW2M2b7gXiFkOUabMVA0LHkRghcFWdjw5mS 0WAnUovlZVUOFe82WG4EEGZZxG9E+Puz2uG7rmnX1nrliJXtKKPCYhoPBY1T/waUybso EI9A== X-Gm-Message-State: AOAM530lDUarNQsTvNOmpRXoP1vLJJkopk1dprE+7zAMo3vFfh5t4r68 d5a/sCgO5i0cjpFueoLeMXo+/MIUuNb5YauqMMQ= X-Google-Smtp-Source: ABdhPJw+gt9LhRLXsrFB+g46rijq73gluhi333CelHYZksHgsYzkzM9kPF6SWPx/yQKnkZqoZ+a/w2mW91J+0aMgt6A= X-Received: by 2002:a67:6746:: with SMTP id b67mr16950370vsc.16.1633979049848; Mon, 11 Oct 2021 12:04:09 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Daniele Varrazzo Date: Mon, 11 Oct 2021 21:03:58 +0200 Message-ID: Subject: Re: psycopg3 transactions To: Paolo De Stefani Cc: Psycopg Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Paolo, in psycopg 3, the normal transaction behaviour demanded by the dbapi is by default enabled. So, even if you don't use `conn.transaction()`, a transaction will be started anyway (just, it won't finish at the end of a block but will need an explicit commit). So, with con.cursor() as cur: cur.execute("SELECT * FROM table;") does actually start a transaction, and if you don't commit it will not be terminated. If you want to use *only* `conn.transaction()` to manage your transactions, and leave everything outside a block as autocommit, you need an autocommit connection, which you can create passing `autocommit=True` on `connect()` or setting `conn.autocommit = True` after connection. Does it help? Cheers -- Daniele On Mon, 11 Oct 2021 at 20:01, Paolo De Stefani wrote: > > Hi all > > In psycopg3 i read and understood the new transaction management > behaviour. With the use of context managers i have to do something like > this: > > con = psycopg.connect() > with con.transaction(): > with con.cursor() as cur: > cur.execute("INSERT INTO table VALUES (1, 2, 3);") > > and this works as expected. > But if i don't need a transaction because i don't need to commit > anything i can do something like this: > > with con.cursor() as cur: > cur.execute("SELECT * FROM table;") > > BUT if a do a select without a transaction the next command that require > a transaction don't works until i do a specific commit > > with con.transaction(): > with con.cursor() as cur: > cur.execute("DELETE FROM table;") > > the delete is effective only for the current connection, i mean other db > user continue to see the without the delete command > modifications > Looks like the previous select statement (uncommited) block following > delete statement even if i use the with con.transaction() statement. > If a do a con.commit() everything works as expected. > > That means i need to use a transaction even for a select statement. > I can't use autocommit connection in my application. > > Is it correct or am i losing anything ??? > > > -- > Paolo De Stefani > >