You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Andre Landwehr <an...@gmx.net> on 2000/02/21 11:55:07 UTC

storing db handles

Hi,
I use Embperl and I have a test- and a production environment which 
use different databases and persistent connections via
Apache::DBI. Since I do not want to change the connection string
in each file I copy from test to production I need either a
global database handle or a mechanism which returns a valid
handle for the current environment. I have two ideas about how to do this:
1) Put the statement into httpd.conf which will make $dbh global
   for all Apache childprocesses. How will Apache::DBI react to
	 this, will it still create new connections if the one
	 predefined in httpd.conf is in use or is Apache::DBI useless
	 in this case?
2) Write a component doing nothing but establishing the db-connection
   and saving the database handle in %udat. The component is then called
	 via 'Execute'. As I understand, a handle can only be used by
	 the one Apache child it was created with, so I have to Execute
	 that component in every page. The loss in performance should
	 not be overwhelming and Apache::DBI should guarantee
	 that still no more db-connections than needed get established.

Since I am not very experienced with this stuff I would like to
hear what the gurus think about this ;-)

Andre


Re: storing db handles

Posted by Andre Landwehr <an...@mini.gt.owl.de>.
On Mon, Feb 21, 2000 at 10:25:30AM -0500, Brendan W. McAdams wrote:
> I did this [my mod_perl is pretty horrible but it works well so bear with
> me] for my startup file:
(...)
>         Apache->push_handlers("PerlChildInitHandler" => sub { $dbh =
> DBI->connect("dbi:mysql:<database>","<username>","<password>"); } );
>         Apache->push_handlers("PerlChildExitHandler" => sub
>  $dbh->disconnect; } );
(...)
> My actual modules for the site have these directives:
> 
> my $dbh ||= DBI->connect("dbi:mysql:$database","$db_user","$db_pass") ||
> return SERVER_ERROR;

Thanks, but this does not really help since I still have the
name, user and password in each of my modules. Is there a way to
access the $dbh from the PerlChildInitHandler directly without
providing this information each time?

Andre


Re: storing db handles

Posted by "Brendan W. McAdams" <br...@plexmedia.com>.
I did this [my mod_perl is pretty horrible but it works well so bear with
me] for my startup file:

#!/usr/local/bin/perl
# Brendan W. McAdams <br...@plexmedia.com>
# Copyright (c) 2000, Plexus InterActive

#init.

BEGIN {
        use lib '/home/httpd/sites/web/cgis/www.artizenuniverse.com/lib';
        use Apache();
        use Apache::DBI;
        Apache->push_handlers("PerlChildInitHandler" => sub { $dbh =
DBI->connect("dbi:mysql:<database>","<username>","<password>"); } );
        Apache->push_handlers("PerlChildExitHandler" => sub
 $dbh->disconnect; } );

}
# includes.
use Apache::Constants();

# globals for apache.

my $dbh;

1;


This then connects a $dbh for each CHILD (globalising the database handler
for the entire apache process is from what i've seen and read dangerous and
unstable - give each child a child scoped handle... I've found this to  be
very stable.
Apache::DBI handles the connection quite well and manages it continually.
My actual modules for the site have these directives:

my $dbh ||= DBI->connect("dbi:mysql:$database","$db_user","$db_pass") ||
return SERVER_ERROR;

hope this helps
-brendan
-----
Brendan W. McAdams                    |  email: brendan@plexmedia.com
Programmer/Systems Administrator  | office: (305)377-2880
Plexus InterActive                            | pager:  (305)277-4879
http://www.plexmedia.com               | cell phone: (305)401-7313


"Always listen to the experts - they'll tell you what can't be done and why.
Then do it."
    -Robert A. Heinlein
----- Original Message -----
From: "Andre Landwehr" <an...@gmx.net>
To: "Brendan W. McAdams" <br...@plexmedia.com>
Sent: Monday, February 21, 2000 9:47 AM
Subject: Re: storing db handles


On Mon, Feb 21, 2000 at 08:08:11AM -0500, Brendan W. McAdams wrote:
> I was approaching a similar problem recently .... I found some info on
Apache::DBI in the Mod_Perl developers guide that may help.
> Basically though I globalised $dbh and then made my connection code this:
>
> $dbh ||= DBI->connect("dbi:mysql:$database","$db_user","$db_pass");
>
> And then setup a child init handler to open up a $dbh on apache startup,
which gives each child a persistant connection.

Could you please show me how to do this? I tried to just write
----
my $dbh = DBI->connect("dbi:Sybase:server=myserver", "user", "pwd",
{PrintError => 0});
die "Unable for connect to myserver: $DBI::errstr" unless $dbhcharts;
-----
into my startup.pl and wrote "use vars qw($dbh)" into my Embperl
pages, but $dbh seems to be undefined.. thanks in advance..

Andre



Re: storing db handles

Posted by "Paul J. Lucas" <pj...@barefooters.org>.
On Mon, 21 Feb 2000, Brendan W. McAdams wrote:

> I found some info on Apache::DBI in the Mod_Perl developers guide that may
> help.  Basically though I globalised $dbh and then made my connection code
> this:
> 
> $dbh ||= DBI->connect("dbi:mysql:$database","$db_user","$db_pass");

	Unless I'm misreading the Apache::DBI manual page, this is
	completely unnecessary.  Apache::DBI makes persistent
	connections transparent so you can call connect() and
	disconnect() as normal.  You don't need to do one-time
	initializations, global variables, or anything.

	- Paul


Re: storing db handles

Posted by "Brendan W. McAdams" <br...@plexmedia.com>.
I was approaching a similar problem recently .... I found some info on Apache::DBI in the Mod_Perl developers guide that may help.
Basically though I globalised $dbh and then made my connection code this:

$dbh ||= DBI->connect("dbi:mysql:$database","$db_user","$db_pass");

And then setup a child init handler to open up a $dbh on apache startup, which gives each child a persistant connection.

I tested it for about 3 days and this was very stable and the database connection very reliable.

-Brendan
--
Brendan W. McAdams                   | Home/Cell: (305)401-7313
Programmer/Systems Administrator  | Work: (305)377-2880
Plexus InterActive                        | email:brendan@plexmedia.com
http://www.plexmedia.com

"Always listen to experts. They'll tell you what can't be done, and why. Then
do it. "
     -- Robert A. Heinlein

----- Original Message ----- 
From: Andre Landwehr <an...@gmx.net>
To: Modperl List <mo...@apache.org>
Sent: Monday, February 21, 2000 5:55 AM
Subject: storing db handles


Hi,
I use Embperl and I have a test- and a production environment which 
use different databases and persistent connections via
Apache::DBI. Since I do not want to change the connection string
in each file I copy from test to production I need either a
global database handle or a mechanism which returns a valid
handle for the current environment. I have two ideas about how to do this:
1) Put the statement into httpd.conf which will make $dbh global
   for all Apache childprocesses. How will Apache::DBI react to
this, will it still create new connections if the one
predefined in httpd.conf is in use or is Apache::DBI useless
in this case?
2) Write a component doing nothing but establishing the db-connection
   and saving the database handle in %udat. The component is then called
via 'Execute'. As I understand, a handle can only be used by
the one Apache child it was created with, so I have to Execute
that component in every page. The loss in performance should
not be overwhelming and Apache::DBI should guarantee
that still no more db-connections than needed get established.

Since I am not very experienced with this stuff I would like to
hear what the gurus think about this ;-)

Andre



Perl Section...

Posted by "Bryan J. Opfer" <op...@ivenue.com>.
Anyone know what this error would mean:

DBI handle cleared whilst still active at
/usr/lib/perl5/site_perl/5.005/i386-linux/DBI.pm line 195.
    dbih_clearcom (h 0x83b1900, com 0x81b6d50):
       FLAGS 0x211: COMSET Warn AutoCommit 
       TYPE 1
       PARENT undef
       KIDS 0 (0 active)
       IMP_DATA undef in 'DBD::mysql::dr'


This error comes up in my apache error log.  I have a Perl section in my
httpd.conf that sets up all my virtual hosts. 

Apache starts up fine, but the virtual hosts are not set up and I get
that error in the error log.  I am using Apache 1.3.12 compiled from
source.

I have the same setup on another machine with Apache 1.3.9 (RPM) and it
works fine.

DBI is version 1.13.

Thanks,
Bryan Opfer