Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
package MyClass::DBI; use strict; use base qw( Class::DBI::Pg ); __PACKAGE__->set_db(Main => 'dbi:Pg:dbname=sometable', 'xxxxxxx', 'xxx +xxx', { PrintError => 1, RaiseError => 1, AutoCommit => 1, ShowErrorStatement => 1, ChopBlanks => 1, } ); sub do_transaction { my($class,$code,@args) = @_; $class->_invalid_object_method('do_transaction()') if ref($class); my @return_values = (); my $dbh = $class->db_Main; # Localize AutoCommit database handle attribute # and turn off for this block. local $dbh->{AutoCommit}; # Note: Leaks memory with Perl 5.6.1. eval { @return_values = $code->(@args); $class->dbi_commit; }; if ($@) { my $error = $@; eval { $class->dbi_rollback; }; if ($@) { my $rollback_error = $@; $class->_croak("Transaction aborted: $error; " . "Rollback failed: $rollback_error\n"); } else { $class->_croak("Transaction aborted (rollback " . "successful): $error\n"); } } return(@return_values); } #eosub--do_transaction
my $user = MyClass::DBI->do_transaction(sub { my($self) = @_; my $model = MyClass::DBI::Users->create({ 'user_id' => $self->user_id, 'email_address' => $self->email_address, }); foreach my $group ($self->groups) { $model->add_to_groups({ 'group_id' => $group }); } return($model); },$self);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Class::DBI - Getting do_transaction() to work
by Prior Nacre V (Hermit) on Sep 25, 2004 at 03:56 UTC | |
|
Re: Class::DBI - Getting do_transaction() to work
by perrin (Chancellor) on Sep 26, 2004 at 04:32 UTC |