DBIx::ScopedTransaction - Scope database transactions on DBI handles in code, to detect and prevent issues with unterminated transactions.
Version 1.2.0
use DBIx::ScopedTransaction; use Try::Tiny; # Optional, define custom logger for errors detected when destroying a # transaction object. By default, this prints to STDERR. $DBIx::ScopedTransaction::DESTROY_LOGGER = sub { my ( $messages ) = @_; foreach my $message ( @$messages ) { warn "DBIx::ScopedTransaction: $message"; } }; # Start a transaction on $dbh - this in turn calls $dbh->begin_work(); my $transaction = DBIx::ScopedTransaction->new( $dbh ); try { # Do some work on $dbh that may succeed or fail. } finally { my @errors = @_; if ( scalar( @errors ) == 0 ) { $transaction->commit() || die 'Failed to commit transaction'; } else { $transaction->rollback() || die 'Failed to roll back transaction.'; } };
Small class designed to be instantiated in a localized scope. Its purpose is to start and then clean up a transaction on a DBI object, while detecting cases where the transaction isn't terminated properly.
The synopsis has an example of working code, let's see here an example in which DBIx::ScopedTransaction helps us to detect a logic error in how the programmer handled terminating the transaction.
sub test { my $transaction = DBIx::ScopedTransaction->new( $dbh ); try { # Do some work on $dbh that may succeed or fail. } catch { $transaction->rollback(); }; } test();
As soon as the test() function has been run, $transaction goes out of scope and gets destroyed by Perl. DBIx::ScopedTransaction subclasses destroy and detects that the underlying transaction has neither been committed nor rolled back, and forces a rollback for safety as well as prints details on what code should be reviewed on STDERR.
Create a new transaction.
my $transaction = DBIx::ScopedTransaction->new( $database_handle, );
Return the database handle the current transaction is operating on.
my $database_handle = $transaction->get_database_handle();
Return whether the current transaction object is active.
# Get the active status of the transaction. my $is_active = $transaction->is_active(); # Set the active status of the transaction. $transaction->is_active( $is_active );
The transaction object goes inactive after a successful commit or rollback.
Commit the current transaction.
my $commit_successful = $transaction->commit();
Roll back the current transaction.
my $rollback_successful = $transaction->rollback();
Log to STDERR warnings and errors that occur when a DBIx::ScopedTransaction object is destroyed.
_default_destroy_logger( $messages );
To override this default logger you can override $DBIx::ScopedTransaction::DESTROY_LOGGER. For example:
$DBIx::ScopedTransaction::DESTROY_LOGGER
$DBIx::ScopedTransaction::DESTROY_LOGGER = sub { my ( $messages ) = @_; foreach $message ( @$messages ) { warn "DBIx::ScopedTransaction: $message"; } };
Clean up function to detect unterminated transactions and try to roll them back safely before destroying the DBIx::ScopedTransaction object.
Please report any bugs or feature requests through the web interface at https://github.com/guillaumeaubert/DBIx-ScopedTransaction/issues/new. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc DBIx::ScopedTransaction
You can also look for information at:
GitHub's request tracker
https://github.com/guillaumeaubert/DBIx-ScopedTransaction/issues
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/DBIx::ScopedTransaction
CPAN Ratings
http://cpanratings.perl.org/d/DBIx::ScopedTransaction
MetaCPAN
https://metacpan.org/release/DBIx-ScopedTransaction
Guillaume Aubert, <aubertg at cpan.org>.
<aubertg at cpan.org>
I originally developed this project for ThinkGeek (http://www.thinkgeek.com/). Thanks for allowing me to open-source it!
Copyright 2012-2017 Guillaume Aubert.
This code is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE file for more details.
To install DBIx::ScopedTransaction, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::ScopedTransaction
CPAN shell
perl -MCPAN -e shell install DBIx::ScopedTransaction
For more information on module installation, please visit the detailed CPAN module installation guide.