I usually set a class level $dbh based on the DB that's
right for the object -- we have several DB's here so there
is no concept of a global $dbh -- it depends on what data
you're dealing with.

I do not like passing it per object method either.  It may
make sense for some methods as an optional override, but 
that's doubtful.

I usually initialize the class level $dbh mentioned above 
and allow an optional new() override per object instance.  
That kind of gives the best of both worlds.  For most of my 
optional overrides, I also provide set/get methods for a full 
OO like interface.  I have some method generator code that 
does this for me -- similar to other things found on CPAN.
So you set up class level, and use that as the instance
default.

In addition, I've also subclassed DBI so I can open by high
level DB names we use here.  In that subclass, I allow $dbh
values to be cached by DB name.  That offers another level 
of handle sharing that extends across any class interfaces.

----
Steve Sapovits
Global Sports Interactive
Work Email: [EMAIL PROTECTED]
Home Email: [EMAIL PROTECTED]
Work Phone: 610-491-7087
Cell:       610-574-7706
Pager:      877-239-4003

> -----Original Message-----
> From: Hardy Merrill [SMTP:[EMAIL PROTECTED]]
> Sent: Tuesday, December 11, 2001 2:14 PM
> To:   [EMAIL PROTECTED]
> Subject:      OO object storage of $dbh?
> 
> I'm new to OO Perl, and have a question about storing the value
> of the $dbh in an object.
> 
> This is a Perl CGI(not CGI.pm) DBI application - each cgi script
> connects at the start and creates a $dbh which then gets used
> throughout the remainder of the script.  For this application,
> I created a class - each cgi script creates a new object of the
> class, and then each script calls various methods on that
> object - many of those methods do database "stuff" that
> obviously require the $dbh.
> 
> My question is, what is the best method for allowing the object
> methods to use the $dbh established at the beginning of a cgi
> script?  The 3 options I saw were:
> 
>   1. pass the $dbh as an argument to each object method
>       - I don't like this method
> 
>   2. pass the $dbh in to the "new" method, and set an instance
>      $DBH variable equal to that $dbh argument - this allows
>      me to access the $DBH instance variable anytime I need
>      access to the database handle
> 
>   3. in the cgi script, once the $dbh is established, call
>      a class method to set a class $DBH variable - and then
>      have each object method invoke a class method to return
>      the value of the class $DBH variable
> 
> I currently have option 2 in effect, but am not really sure
> this is the best method.  Anyone else see other methods that
> I could use, or have an opinion on which of the 3 options
> above is best?  Are there memory(leak?) issues with storing
> the $dbh in each object, or memory issues with any of the
> options?  Any help is appreciated.
> 
> -- 
> Hardy Merrill
> Mission Critical Linux, Inc.
> http://www.missioncriticallinux.com

Reply via email to