Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

New relatively new to OOP (I've danced with it a few times over the last couple of years but keep giving up after I get in over my head). I'm also very new to Class::DBI.

I'm having a problem. According to http://www.class-dbi.com/cgi-bin/wiki/index.cgi?AtomicUpdates, I need to use the following code and place it in (I think?) my subclass.

This is my subclass.
I'm not sure if do_transaction() belongs here or not? I think it does . . . ? And I can't figure out how to properly refer to my database handle from within do_transaction(), since do_transaction() refers to $dbh and I don't have a $dbh. In the main script, if I want to access my database handle, I do something like my $use = MyClass::DBI::User->retrieve($uid).

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


Then, from my main program/script.
Then, I place this code in my program when I want to use a transaction. When I actually try to run it, I get a complaint referring to the last '$self' (},$self);.

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);


I thought I was doing so well, until I started to mess with adding do_transaction(). Now I'm ready to pull my hair out!! ARGH!

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

    It looks like $self is out of scope (hard to tell with out seeing surrounding code). If this is the problem, change the order of these two lines to fix:

    my $user = MyClass::DBI->do_transaction(sub { my($self) = @_;

    Regards,

    PN5

Re: Class::DBI - Getting do_transaction() to work
by perrin (Chancellor) on Sep 26, 2004 at 04:32 UTC
    Just to be clear, you do not need to use do_transaction() in order to use transactions with Class::DBI. It isn't even a part of Class::DBI; it's something that merlyn came up with to make things more convenient for himself and then shared with us on the Class::DBI list. All you have to do to use transactions is call commit like you normally would after doing some database work:
    MyClass::DBI->dbi_commit();