Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: RFC: transactions.pm

by Juerd (Abbot)
on Apr 27, 2003 at 20:19 UTC ( [id://253527]=note: print w/replies, xml ) Need Help??


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

Well, personally, I would just put it under DBIx::Transaction. It's just a syntactic sugar module, and doesn't actually implement any kind of transactions. Calling it Transactions could be misleading to people.

I'm open to suggestions, but not to DBIx::. This module has a much wider scope. Currently, I know of no other modules that use begin_work, rollback and commit, but in theory any transaction capable module could be used with this.

This sample code doesn't actually work, does it? The use transactions part would be executed at compile time before $dbh has a value.

That's correct. I tested without a database, and was lazy so I supplied a string literal. I thought it was possible to reference the container instead of the value, but I was wrong. No, the module doesn't work.

If you only pass in $dbh at compile time (because it's done with a use), the $dbh inside transactions will never get updated.

That will have to be changed.

Defining your subs inline like that looks pretty strange.

That's because they're closures that share the same $dbh. Kind of useless if you consider that $dbh is a reference to undef, but I didn't know that at that time.

I don't think you need to take a reference to $_1, since it is a ref already.

That was because it was used in other subs, which have @_'s of their own. Needed to reference to avoid copying.

Creating a routine that takes sub refs but does not appear to (like transaction()) leads to many scoping bugs.

I will document that. Sourcefilters are an ugly solution. I'd rather introduce one very obvious bug than lots of subtle parsing bugs.

Maybe I'll make the module OO after all, but I think this looks ugly:

my $t = Transaction->new($dbh); $t->transaction(sub { for (1..10) { my $sth = $dbh->prepare(...); $sth->execute() or $t->rollback; } });
Or I can just have a function called transaction_select (lots of typing - better names welcome)
transaction_select $dbh; transaction { for (1..10) { my $sth = $dbh->prepare(...); $sth->execute() or rollback; } };
One thing's for sure: the module as pasted in this thread's root node does not work and needs to be fixed. Thanks for pointing that out.

Update: new version, with working code. I thought the OO solution was way too ugly and I didn't like transaction_select either. I decided to use a package global $T instead.

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

Replies are listed 'Best First'.
Re: Re: Re: RFC: transactions.pm
by particle (Vicar) on Apr 28, 2003 at 17:27 UTC
    I'm open to suggestions, but not to DBIx::. This module has a much wider scope. Currently, I know of no other modules that use begin_work, rollback and commit, but in theory any transaction capable module could be used with this.

    ...then don't tie people in to the method names, provide them with the ability to redefine them. a caller might do:

    use My::Transactions 0.01 begin_work => try_it, rollback => fallback, commit => do_it;

    ~Particle *accelerates*

      ...then don't tie people in to the method names, provide them with the ability to redefine them.

      Subclass or wrap all you want. Perl already has perfect features to support what you wish :)

      Here are three examples to accomplish this task, but there a lot of other ways to do it.

      # 1) subclassing package Foo::Bar::Sub; use base 'Foo::Bar'; sub begin_work { shift->try_it } sub commit { shift->do_it } sub begin_work { shift->try_it } # 2) wrapping package Foo::Bar::Wrap; use Attribute::Property; use Scalar::Util qw(blessed) sub new : New; sub object : Property { blessed } sub begin_work { shift->object->try_it } sub commit { shift->object->do_it } sub rollback { shift->object->fallback } # 3) intruding sub Foo::Bar::begin_work { shift->try_it } sub Foo::Bar::commit { shift->do_it } sub Foo::Bar::rollback { shift->fallback }

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-24 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found