Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: RFC: Transactions.pm (select function)

by Aristotle (Chancellor)
on Apr 28, 2003 at 11:03 UTC ( [id://253649]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: RFC: Transactions.pm
in thread RFC: Transactions.pm

If the goal is a module that can be used in multiple instances though then a package global is a horrible interface to begin with. How about something like this?
use Exporter::Tidy default => [ qw(transaction select_handle commit ro +llback) ]; sub select_handle { require Carp; Carp::croak("Can't select handle outside transaction"); } sub _cant_change { require Carp; Carp::croak("Can't change handle during transaction"); } sub transaction (&) { my ($block) = @_; my ($caller, $handle); my $caller = caller(); local *{"${caller}::select_handle"} = sub { $handle = shift; *{"${caller}::select_handle"} = \&_cant_change; }; # ... }
To use it you then have to say
transaction { select_handle($dhb); # ... };
which allows to use the module for as many handles even within the same package as you like. You can even nest transactions with no adverse effects.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re^3: RFC: Transactions.pm
by Juerd (Abbot) on Apr 28, 2003 at 11:15 UTC

    transaction { select_handle($dhb); ... };

    Not an option, because you need the handle before you begin the transaction. Won't begin the transaction inside the block, because that would allow stuff to be done outside the transaction while visually, it's in the transaction code block.

    Simple to use modules have their limitations. The limitation of this one is that you shouldn't use nested transactions. I think that's acceptable in most cases.

    I've only seen nested transactions being used to rollback multiple things. But it's easy to create a wrapper module for that:

    package NeedsAName; sub new { return bless \@_, shift } sub AUTOLOAD { $_->$AUTOLOAD(@_) for @{+shift}; } package main; use Transactions; my $dbh1 = DBI->connect(...); my $dbh2 = DBI->connect(...); our $T = NeedsAName->new($dbh1, $dbh2); transaction { ... };

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      Won't begin the transaction inside the block, because that would allow stuff to be done outside the transaction while visually, it's in the transaction code block.
      By the same token you can calculate the question to 42 after you finish working with the handle but before you leave the transaction block. You're simply going to have to rely on discipline on that one. Perl always does so a bit more than other languages anyway.

      Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://253649]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2024-04-25 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found