What if there's an exception in your exception handler? I'd put the
rollback in the 'finally' handler, so it's always called. If you've
already called 'commit' then the rollback does nothing of course.
I would much prefer to have just *one* place where exceptions are caught
and logged.
On Sun, 8 Sep 2024 11:03:21 -0000 (UTC), Jon Ribbens wrote:
What if there's an exception in your exception handler? I'd put the
rollback in the 'finally' handler, so it's always called. If you've
already called 'commit' then the rollback does nothing of course.
In any DBMS worth its salt, rollback is something that happens
automatically if the transaction should fail to complete for any reason.
This applies for any failure reason, up to and including a program or
system crash.
On 2024-09-08, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
On Sun, 8 Sep 2024 11:03:21 -0000 (UTC), Jon Ribbens wrote:
What if there's an exception in your exception handler? I'd put the
rollback in the 'finally' handler, so it's always called. If you've
already called 'commit' then the rollback does nothing of course.
In any DBMS worth its salt, rollback is something that happens
automatically if the transaction should fail to complete for any
reason.
This applies for any failure reason, up to and including a program or
system crash.
If it's a program or system crash, sure, but anything less than that -
how would the database even know, unless the program told it?
Right, but that's true only when writable connections areThe database only needs to commit when it is explicitly told. Anything
less -- no commit.
So the Python code is half-way through a transaction when it throws
a (non-database-related) exception and that thread of execution is
aborted. The database connection returns to the pool, and is re-used
by another thread which continues using it to perform a different
sequence of operations ... ending in a COMMIT, which commits
one-and-a-half transactions.
So the Python code is half-way through a transaction when it throwsHow does it return to the pool ?
a (non-database-related) exception and that thread of execution is
aborted. The database connection returns to the pool,
On 2024-09-09, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
The database only needs to commit when it is explicitly told. Anything
less -- no commit.
So the Python code is half-way through a transaction when it throws a (non-database-related) exception and that thread of execution is
aborted. The database connection returns to the pool ...
On Mon, 9 Sep 2024 10:00:11 -0000 (UTC), Jon Ribbens wrote:
On 2024-09-09, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
The database only needs to commit when it is explicitly told. Anything
less -- no commit.
So the Python code is half-way through a transaction when it throws a
(non-database-related) exception and that thread of execution is
aborted. The database connection returns to the pool ...
The DBMS connection is deleted.
On 2024-09-09, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
On Mon, 9 Sep 2024 10:00:11 -0000 (UTC), Jon Ribbens wrote:
On 2024-09-09, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
The database only needs to commit when it is explicitly told.
Anything less -- no commit.
So the Python code is half-way through a transaction when it throws a
(non-database-related) exception and that thread of execution is
aborted. The database connection returns to the pool ...
The DBMS connection is deleted.
How does that happen then?
On Mon, 9 Sep 2024 21:12:51 -0000 (UTC), Jon Ribbens wrote:
On 2024-09-09, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
On Mon, 9 Sep 2024 10:00:11 -0000 (UTC), Jon Ribbens wrote:
On 2024-09-09, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
The database only needs to commit when it is explicitly told.
Anything less -- no commit.
So the Python code is half-way through a transaction when it throws a
(non-database-related) exception and that thread of execution is
aborted. The database connection returns to the pool ...
The DBMS connection is deleted.
How does that happen then?
You write code to do it.
Ok. So we've moved away from "In any DBMS worth its salt, rollback is something that happens automatically"Nope. The original post asked something entirely different.
and now you're saying it isn't automatic after all,No again, such shenanigans only start to happen when pooling
Am Tue, Sep 10, 2024 at 08:38:30AM -0000 schrieb Jon Ribbens via Python-list:
Ok. So we've moved away from "In any DBMS worth its salt, rollback is
something that happens automatically"
Nope. The original post asked something entirely different.
and now you're saying it isn't automatic after all,
No again, such shenanigans only start to happen when pooling
is brought into the equation.
On Tue, 10 Sep 2024 08:38:30 -0000 (UTC), Jon Ribbens wrote:
On 2024-09-09, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
On Mon, 9 Sep 2024 21:12:51 -0000 (UTC), Jon Ribbens wrote:
On 2024-09-09, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
On Mon, 9 Sep 2024 10:00:11 -0000 (UTC), Jon Ribbens wrote:
On 2024-09-09, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
The database only needs to commit when it is explicitly told.
Anything less -- no commit.
So the Python code is half-way through a transaction when it throws >>>>>> a (non-database-related) exception and that thread of execution is >>>>>> aborted. The database connection returns to the pool ...
The DBMS connection is deleted.
How does that happen then?
You write code to do it.
Ok. So we've moved away from "In any DBMS worth its salt, rollback is
something that happens automatically" and now you're saying it isn't
automatic after all, "you write code to do it".
The database code already performs that function. As far as the client is concerned, the function happens automatically.
And it’s not just code, it’s data. The database structures on persistent storage are also carefully designed with transaction safety in mind. So
any partial transaction data saved on persistent storage that remains
after a system crash can be identified as such and discarded, leaving the database in its pre-transaction state.
On Tue, 10 Sep 2024 22:48:36 -0000 (UTC), Jon Ribbens wrote:
But what if you tell it the wrong thing ...
To get back to the original point of this thread, all that rigmarole to
try to ensure to call “rollback” in case of an exception is completely unnecessary: the DBMS will take care of that for you.
This is how I normally do things like this:
try:
do something .commit()
except:
log something .rollback()
Doing an explicit rollback ensures that the transaction is always rolled
back if it is interrupted for any reason.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 991 |
Nodes: | 10 (1 / 9) |
Uptime: | 134:56:23 |
Calls: | 12,961 |
Calls today: | 3 |
Files: | 186,574 |
Messages: | 3,266,257 |