pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Siddharth Jain <siddhsql@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: what happens if a failed transaction is not rolled back?
Date: Mon, 24 Apr 2023 08:45:03 -0700
Message-ID: <89614320-852e-ce8d-7b01-2eff0e537e09@aklaver.com> (raw)
In-Reply-To: <d02fa181-e7a7-75bb-57ce-097c09835cb4@aklaver.com>
References: <CAPqV3pQms8Z5-v7vxZ9pRMxf7n0wOW+XUZ9cWjBP5=Fu=4_NFA@mail.gmail.com>
	<d02fa181-e7a7-75bb-57ce-097c09835cb4@aklaver.com>

On 4/24/23 08:43, Adrian Klaver wrote:
> On 4/24/23 08:37, Siddharth Jain wrote:
>> Hi All,
>>
>> i understand when writing application code, we should rollback a 
>> transaction that fails to commit. this is typically done in the catch 
>> block of a try-catch exception handler. but what if the developer does 
>> not rollback the transaction? what happens in that case?
>>
>> note that i am not asking: what happens if a transaction is not rolled 
>> back?
>> i am asking: what happens if a /failed/ transaction is not rolled back?
>>
>> failed transaction = you try to commit it but get an exception back 
>> from the database.
> 
> In Python:
> 
> import psycopg2
> con = psycopg2.connect("dbname=test host=localhost  user=postgres")
> cur = con.cursor()
> cur.execute("select 1/0")
> DivisionByZero: division by zero
> 
> cur.execute("select 1")
> InFailedSqlTransaction: current transaction is aborted, commands ignored 
> until end of transaction block

Forgot to add.

To get past above:

con.rollback()
cur.execute("select 1")

> 
> 
> 
>>
>> thanks.
>>
>> S.
> 

-- 
Adrian Klaver
adrian.klaver@aklaver.com






view thread (11+ messages)  latest in thread

Message-ID: <89614320-852e-ce8d-7b01-2eff0e537e09@aklaver.com>
Permalink:  ../89614320-852e-ce8d-7b01-2eff0e537e09@aklaver.com/
Also on:    postgresql.org/message-id/89614320-852e-ce8d-7b01-2eff0e537e09@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, siddhsql@gmail.com
  Subject: Re: what happens if a failed transaction is not rolled back?
  In-Reply-To: <89614320-852e-ce8d-7b01-2eff0e537e09@aklaver.com>
  References: <CAPqV3pQms8Z5-v7vxZ9pRMxf7n0wOW+XUZ9cWjBP5=Fu=4_NFA@mail.gmail.com> <d02fa181-e7a7-75bb-57ce-097c09835cb4@aklaver.com> <89614320-852e-ce8d-7b01-2eff0e537e09@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox