NAME
DBIx::QuickORM::Txn - A transaction or savepoint.
DESCRIPTION
Returned by $orm->txn, and given to the block of $orm->txn(sub { ... }). Finish it with commit or rollback. The outermost transaction is a database transaction. One opened while another is open is a savepoint inside it. See "Transactions" in DBIx::QuickORM for how transactions affect the rows the ORM holds.
The outermost transaction opens at an explicit isolation level, 'read committed' unless the ORM or the txn call names another (SQLite and DuckDB have none to set). A savepoint runs at its transaction's level. See "ISOLATION" in DBIx::QuickORM::Manual::Transactions.
Only the innermost open transaction may be committed or rolled back. One is finished once the database has confirmed its commit or rollback.
A statement that fails inside a transaction marks the innermost one failed, even when the error is caught. Then every statement on the connection croaks, except the rollback, and commit rolls back instead and croaks. Rolling back a savepoint clears its mark, so a nested transaction around work that may fail is the way to recover and go on. See "A FAILED STATEMENT" in DBIx::QuickORM::Manual::Transactions.
An unfinished transaction that goes out of scope is rolled back, together with any transaction still open inside it, with a warning naming where it was opened. An async query still pending is cancelled first. If the rollback fails, the warning says so and the transaction is dropped from the stack anyway.
Whenever a transaction's outcome is unknown like this, no row cached from inside the root transaction stays canonical. Rows cached from before it that the transaction did not touch do. Refresh a row before writing it again.
Row state (is_in_db, was_deleted) then follows the rollback rules, but that is a guess: the database may have committed. Re-read the rows the transaction touched.
One still open at program exit is left to the database, which discards it when the connection closes. One inherited by a forked child does nothing there, issues no SQL, and croaks if committed or rolled back.
SYNOPSIS
Block form
# Commits when the block returns. An exception rolls back and is rethrown.
$orm->txn(sub {
my $bob = $orm->insert(users => {name => 'Bob'});
$orm->insert(
posts => {
user_id => $bob->field('id'),
title => 'Hello',
},
);
});
# Commit or roll back early through the transaction the block is given.
$orm->txn(sub {
my $txn = shift;
$orm->insert(users => {name => 'Temp'});
$txn->rollback;
});
Object form
my $txn = $orm->txn;
$orm->insert(users => {name => 'Carol'});
$txn->commit;
my $undo = $orm->txn;
$orm->insert(users => {name => 'Nobody'});
$undo->rollback;
ATTRIBUTES
- $conn = $txn->connection
-
The DBIx::QuickORM::Connection that opened it.
- $name = $txn->name
-
Unique within its Connection, and used as a savepoint's name in SQL.
- $depth = $txn->depth
-
1 for the outermost transaction, 2 for a savepoint inside it, and so on.
- $frame = $txn->caller
-
Where it was opened, as
callerreturns it:[$package, $file, $line]. - $error = $txn->failed
-
The error of the statement that marked it failed, or undef.
PUBLIC METHODS
- $txn->commit
-
Commits the transaction, or releases the savepoint. Returns 1.
On a transaction marked failed it rolls back instead, then croaks.
When the database refuses, the transaction is rolled back and the database's error rethrown. When the rollback fails too, the transaction is dropped from the stack, no row cached from inside the root transaction stays canonical, and the croak says its outcome is unknown.
- $txn->rollback
-
Rolls back the transaction or savepoint. Returns 1.
When the database refuses, the transaction stays open and the error is thrown.
Both croak when the transaction is finished, when one opened inside it is still open, while an async query is pending on the connection, or when called in a process other than the one that opened it.
SOURCE
The source code repository for DBIx-QuickORM can be found at https://github.com/exodist/DBIx-QuickORM.
MAINTAINERS
AUTHORS
COPYRIGHT
Copyright Chad Granum <exodist7@gmail.com>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.