The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


DB::Transaction - feather-weight transaction management for your DBI handles


    use DB::Transaction qw(run_in_transaction);

    my $dbh = My::Application->get_dbh;

    run_in_transaction {
            update risky_business -- in some fashion
    } db_handle => $dbh, on_error => 'rollback';


DB::Transaction provides one function: run_in_transaction


By default, none. On request, run_in_transaction.

run_in_transaction BLOCK db_handle => $db_handle, on_error => ['rollback' | 'continue']

Begin a transaction on $db_handle, then run BLOCK. Any errors raised in the course of executing BLOCK will cause the current transaction to be handled according to your on_error specification.

on_error may be one of these two options:

  • rollback -- call this dbh's ->rollback method

  • continue -- just keep on chugging, man!

on_error => 'rollback' is the default behavior.

Transactions may be nested, though your underlying database may not support nested transactions. It's up to you to know whether this is supported or not.


To contribute back to this project, log in to your GitHub account and visit, then fork the repository.

Create a feature branch, make your changes, push them back to your fork, and submit a pull request via GitHub.

    # fork the project in github

    git clone git://<your-name>/perl-db-transaction.git
    git checkout -b feature-add-spiffy-functionality

    emacs -nw t/spiffy-functionality.t   # hack hack hack
    emacs -nw lib/DB/      # hack hack hack

    git push feature-add-spiffy-functionality origin

    # submit pull request via github


Written by Aaron Cohen <> and Belden Lyman <> at Shutterstock, Inc. Released to CPAN by Shutterstock, Inc.

If you like the idea of working at a company that supports open-source development, why not checkout our jobs page and drop us a line?


    (c) 2013 Shutterstock, Inc. All rights reserved.

This library is free software: you may redistribute it and/or modify it under the same terms as Perl itself; either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.