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

My question is similar to this one posted 1 year ago Passing Database handles to other modules.

I'm building a little application in mod_perl, and have written the bulk of database connectivity into its own package (myApp::DB) , so that I can easily access database handles.

Currently, I open a connection to the database in the 'main' perl script that sets the apache handler. ( my $dbh = myApp::DB->dbconnect(); # returns a ref)

I then either pass the reference around to supplementary packages that need it.

I've been working on a custom form processing package however, that needs to handle certain SQL select statements.

Which is where my problem now comes in:
Right now, the form validation package is built as a generic package that I can reuse. I'm passing in a ref to the dbh when needed -- and that works fine, but its a little annoying, as I feel that I'm passing the ref around alot.

I've considered subclassing my form validator, to have a getDBH function on an init. I've also considered not passing refs around, and just calling connects repeatedly, and letting apache::dbi handle the intercepting.

Both those solutions seem equally as inefficient as the one I've currently got.

Can anyone reccommend something that makes more sense?
  • Comment on Passing Database handles to other modules. #2

Replies are listed 'Best First'.
Re: Passing Database handles to other modules. #2
by diotalevi (Canon) on May 10, 2004 at 21:17 UTC
    I wrote Class::WeakSingleton just so I could have a handle that would be everywhere I wanted it but would still go away when I threw away all the references to it.
Re: Passing Database handles to other modules. #2
by perrin (Chancellor) on May 10, 2004 at 20:49 UTC
    I typically just have an accessor, similar to yours, for grabbing a dbh. Then I can call it whenever I need to, and never pass around the handle. If you're using Apache::DBI, it's lightweight, and if you are really worried about it you can cache the dbh in $r->pnotes(), which gets automatically cleared after every request.
Re: Passing Database handles to other modules. #2
by adrianh (Chancellor) on May 10, 2004 at 21:44 UTC
    Right now, the form validation package is built as a generic package that I can reuse. I'm passing in a ref to the dbh when needed -- and that works fine, but its a little annoying, as I feel that I'm passing the ref around alot.

    Is the form validation package functional/procedural or object oriented? If the former maybe you want to consider moving to the latter so you can have the $dbh become part of the object state and only pass it the once at object creation time.

      It is indeed oop, and thats a method that i've played with!
        It is indeed oop, and thats a method that i've played with!

        In that case, if you're still finding that you're creating lots of instances of your validation object in different places, I'd suspect that there is a layer of abstraction that's being missed somewhere.

        Are their other arguments that are similar between multiple instances? Would it make sense to make a factory object of some kind for creating similar validation objects?

        I'd tend to want to keep passing in the ref somewhere because it keeps everything decoupled and makes testing easier.