Dmitry Karasik > DBIx-Roles-1.04 > DBIx::Roles::Transaction



Annotate this POD

View/Report Bugs
Module Version: 1.00   Source  


DBIx::Roles::Transaction - allow nested transactions.


Wraps begin_work, rollback, and commit calls so that these can be called inside transactions. If an inner transaction calls rollback, all outer transactions fail. The original idea appeared in DBIx::Transactions by Tyler MacDonald.


     use DBIx::Roles qw(Transaction);

     my $dbh = DBI-> connect(
     sub do_something {
         my($dbh, $num) = @_;
         if($dbh->do("DO SOMETHING IN SQL WHERE num = $num")) {
         } else {
      for my $i (1 .. 10) {
         do_something($dbh, $i);
      if( $dbh->commit) {
         print "Every nested transaction worked and the database has been saved.\n";
      } else {
         print "A nested transaction rolled back, so nothing happened.\n";


The role is useful, if you planning a library where methods are required to be transactions, but also might be called from within a transaction. For example, a method changing user password can be implemented as a transaction, but also can be called from the method that adds user data, which is in turn can be a transaction too.

The role has nothing to do with the real nested transactions, that might be implemented by a particular database engine, such as for example savepoints in PosgtreSQL.


DBI, DBIx::Roles, DBIx::Transaction.


Copyright (c) 2005 catpipe Systems ApS. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Dmitry Karasik <>

syntax highlighting: