DBIx::TransactionManager - transaction handling for database.
basic usage:
use DBI; use DBIx::TransactionManager; my $dbh = DBI->connect('dbi:SQLite:'); my $tm = DBIx::TransactionManager->new($dbh); $tm->txn_begin; $dbh->do("insert into foo (id, var) values (1,'baz')"); $tm->txn_commit;
scope_gurad usage:
use DBI; use DBIx::TransactionManager; my $dbh = DBI->connect('dbi:SQLite:'); my $tm = DBIx::TransactionManager->new($dbh); my $txn = $tm->txn_scope; $dbh->do("insert into foo (id, var) values (1,'baz')"); $txn->commit;
nested transaction usage:
use DBI; use DBIx::TransactionManager; my $dbh = DBI->connect('dbi:SQLite:'); my $tm = DBIx::TransactionManager->new($dbh); { my $txn = $tm->txn_scope; $dbh->do("insert into foo (id, var) values (1,'baz')"); { my $txn2 = $tm->txn_scope; $dbh->do("insert into foo (id, var) values (2,'bab')"); $txn2->commit; } { my $txn3 = $tm->txn_scope; $dbh->do("insert into foo (id, var) values (3,'bee')"); $txn3->commit; } $txn->commit; }
DBIx::TransactionManager is a simple transaction manager. like DBIx::Class::Storage::TxnScopeGuard.
get DBIx::TransactionManager's instance object. $dbh parameter must be required.
get DBIx::TransactionManager::ScopeGuard's instance object. You may pass an optional argument to %args, to tell the scope guard where the scope was generated, like so:
sub mymethod { my $self = shift; my $txn = $tm->txn_scope( caller => [ caller() ] ); } $self->mymethod();
This will allow the guard object to report the caller's location from the perspective of mymethod(), not where txn_scope() was called.
mymethod()
txn_scope()
see "DBIx::TransactionManager::ScopeGuard's METHODS"
Start the transaction.
txn_begin may optionally take a 'caller' argument. This will allow you to provide caller information which will be used in in_transaction. For example if you have a wrapper function that calls txn_begin, you may want to let the user think that the caller was one stack above your wrapper.
txn_begin
in_transaction
# use __my__ caller! $tm->txn_begin( caller => [ caller(0) ] );
Rollback the transaction.
Commit the transaction.
Returns true if $txn is currently in a middle of a transaction. While normally you only need to use this value as a boolean, it actually returns a hashref consisting of 'caller' and 'pid' element. This will tell you exactly where the currently valid transaction started.
Atsushi Kobayashi <nekokak _at_ gmail _dot_ com>
DBIx::Class::Storage::TxnScopeGuard
DBIx::Skinny::Transaction
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install DBIx::TransactionManager, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::TransactionManager
CPAN shell
perl -MCPAN -e shell install DBIx::TransactionManager
For more information on module installation, please visit the detailed CPAN module installation guide.