NAME

DBIx::QuickORM::Connection::Transaction - One transaction or savepoint on a DBIx::QuickORM connection.

DESCRIPTION

Represents a single transaction (or savepoint) and the callbacks queued against it. commit and rollback record the outcome and break out of the enclosing QORM_TRANSACTION loop; terminate records the final result and fires the queued success / fail / completion callbacks. An optional finalize callback runs when the transaction completes or, as a safety net, when the object is destroyed while still pending.

SYNOPSIS

QORM_TRANSACTION: {
    my $txn = DBIx::QuickORM::Connection::Transaction->new(id => $id);
    $txn->add_success_callback(sub { ... });
    ...
    $txn->commit;
}

ATTRIBUTES

id

The transaction identifier (required).

savepoint

True when this represents a savepoint rather than a top-level transaction.

on_success
on_fail
on_completion

Callback queues (arrayrefs, or a single coderef normalized to one) fired by terminate. Success or fail callbacks run depending on the outcome, followed by completion callbacks in both cases.

verbose

When true, commit / rollback warn a trace line. A string longer than one character is used as the transaction name in that warning.

result

Undef while open; 1 on success, 0 on failure once terminated.

errors

The error(s) captured on failure.

trace

Arrayref describing where the transaction was started, used in throw.

exception

The exception that forced the transaction to roll back, if any. Set when the transaction's body threw (or the transaction fell out of scope); undef for a normal commit or an explicit rollback.

in_destroy

True while finalize runs from DESTROY.

finalize

The finalize callback, if set.

no_last

When true, commit / rollback skip the last QORM_TRANSACTION jump.

PUBLIC METHODS

$bool = $txn->is_savepoint

True when this is a savepoint.

$bool = $txn->complete

True once a result has been recorded.

$str = $txn->state

Returns active while the transaction is open, then committed or rolled_back once it finishes. Derived from result.

$bool = $txn->committed

True if the transaction committed, false if it rolled back, undef while still open. Derived from result.

$bool = $txn->rolled_back

The inverse of committed: true if it rolled back, false if it committed, undef while still open.

$bool = $txn->aborted

True if an explicit rollback was requested on this transaction.

$txn->rollback
$txn->rollback($why)
$txn->abort
$txn->abort($why)

Records the rollback (optionally with a reason), runs finalize when set, and breaks out of the enclosing QORM_TRANSACTION loop unless no_last is set. abort is an alias for rollback.

$txn->commit
$txn->commit($why)

Records the commit (optionally with a reason), runs finalize when set, and breaks out of the enclosing QORM_TRANSACTION loop unless no_last is set.

($ok, $errors) = $txn->terminate($res, $err)

Records the final result, clears the callback queues and savepoint, then runs the success-or-fail callbacks followed by the completion callbacks. Returns a list: a boolean for whether all callbacks succeeded, and an arrayref of any callback errors (undef when none).

$txn->add_success_callback($cb)
$txn->add_fail_callback($cb)
$txn->add_completion_callback($cb)

Queue a callback to run from terminate on success, on failure, or in both cases respectively.

$txn->throw($err)

Confesses with $err annotated by where the transaction was started, noting when the throw happens during DESTROY.

$txn->set_finalize($cb)

Sets the finalize callback.

$ok = $txn->finalize($ok, $err)

Runs and clears the finalize callback, passing it the transaction, $ok, and $err; returns $ok. Croaks when there is no finalize callback set.

SOURCE

The source code repository for DBIx::QuickORM can be found at https://github.com/exodist/DBIx-QuickORM.

MAINTAINERS

Chad Granum <exodist@cpan.org>

AUTHORS

Chad Granum <exodist@cpan.org>

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.

See https://dev.perl.org/licenses/