persistant db connection

  • Thread starter Alexandre Jaquet
  • Start date
A

Alexandre Jaquet

I'm a bit confused where I should place persistant connection and I want
my scripts useds as compiled registry scripts.

I've added in my httpd.conf

PerlModule Apache::DB;

<Location /perl-status>
SetHandler perl-script
PerlResponseHandler Apache::Status
</Location>
<Location /script>
SetHandler perl-script
PerlHandler ModPerl::Registry
Options ExecCGI
PerlSendHeader On
Order allow,deny
Allow from all
</Location>




I add on my startup.pl script :

use strict;
use ModPerl::Registry ();
use Apache2 ();
use Apache::DBI;
use Image::Magick;
use DBI;
use Mail::Sender;
use Digest::MD5 qw(md5_hex);
use Data::Dumper;
use POSIX;
use CGI ();
CGI->compile(':all');

Apache::DBI->connect_on_init
("DBI:mysql:test:localhost",
"username",
"passwd",
{
PrintError => 1, # warn() on errors
RaiseError => 0, # don't die on error
AutoCommit => 1, # commit executes immediately
}
);
return 1;

but now how can I use it in my scripts who needs to use this connection
? for exemple I consider $dbh as the connection reference

$dbh->do($sql) ...
 
B

Brian McCauley

Alexandre said:
I'm a bit confused where I should place persistant connection and I want
my scripts useds as compiled registry scripts.

I've added in my httpd.conf

PerlModule Apache::DB;

Are you sure you don't mean Apache::DBI?
I add on my startup.pl script :

use strict;
use ModPerl::Registry ();
use Apache2 ();
use Apache::DBI;
use Image::Magick;
use DBI;
use Mail::Sender;
use Digest::MD5 qw(md5_hex);
use Data::Dumper;
use POSIX;
use CGI ();
CGI->compile(':all');

Apache::DBI->connect_on_init
("DBI:mysql:test:localhost",
"username",
"passwd",
{
PrintError => 1, # warn() on errors
RaiseError => 0, # don't die on error
AutoCommit => 1, # commit executes immediately
}
);
return 1;

but now how can I use it in my scripts who needs to use this connection
? for exemple I consider $dbh as the connection reference

I've never used connect_on_init() but usually Apache::DBI is completely
transparent. You simply call DBI->connect and Apache::DBI will
intercept the request and return a persistant connection if there is on
with matching connection information.
 
A

Alexandre Jaquet

Brian McCauley a écrit :
Are you sure you don't mean Apache::DBI?



I've never used connect_on_init() but usually Apache::DBI is completely
transparent. You simply call DBI->connect and Apache::DBI will
intercept the request and return a persistant connection if there is on
with matching connection information.

Thanks :)
 
A

Alexandre Jaquet

Alexandre Jaquet a écrit :
Brian McCauley a écrit :


Thanks :)

But now I'm asking about how to solve this :

I got in my environement multiple web sites and databaes. How can I deal
with this ?

thx in advance
 
B

Brian McCauley

Alexandre Jaquet wrote:

[ excessive quotage - please include just enough to give context ]
But now I'm asking about how to solve this :

I got in my environement multiple web sites and databaes. How can I deal
with this ?

Can you explain why you perceive there to be a problem?

Apache::DBI will maintain one persistant connection for each database
for each Perl interpreter.
 
A

Alexandre Jaquet

Brian McCauley a écrit :
Alexandre Jaquet wrote:

[ excessive quotage - please include just enough to give context ]
But now I'm asking about how to solve this :

I got in my environement multiple web sites and databaes. How can I
deal with this ?


Can you explain why you perceive there to be a problem?

Apache::DBI will maintain one persistant connection for each database
for each Perl interpreter.

In the startup.pl I specify DBI:mysql:test : If I want it's work for

db1, db2 ... ?


Apache::DBI->connect
("DBI:mysql:test:localhost",
"dbmaster",
"tbontb22",
{
PrintError => 1, # warn() on errors
RaiseError => 0, # don't die on error
AutoCommit => 1, # commit executes immediately
}
);
 
A

Alexandre Jaquet

Alexandre Jaquet a écrit :
Brian McCauley a écrit :
Alexandre Jaquet wrote:

[ excessive quotage - please include just enough to give context ]
But now I'm asking about how to solve this :

I got in my environement multiple web sites and databaes. How can I
deal with this ?



Can you explain why you perceive there to be a problem?

Apache::DBI will maintain one persistant connection for each database
for each Perl interpreter.

In the startup.pl I specify DBI:mysql:test : If I want it's work for

db1, db2 ... ?


Apache::DBI->connect
("DBI:mysql:test:localhost",
"dbmaster",
"tbontb22",
{
PrintError => 1, # warn() on errors
RaiseError => 0, # don't die on error
AutoCommit => 1, # commit executes immediately
}
);

Do I've to execute show databases and do a loop with each database ?
 
K

Keith Keller

Alexandre Jaquet a écrit :

Do I've to execute show databases and do a loop with each database ?

First, DBI->connect does not go in startup.pl! DBI->connect is what
you use in your CGI/mod_perl scripts. connect_on_init is used in
startup.pl (assuming that your startup.pl is called in your httpd.conf).

Second, as Brian already suggested, you don't have to connect_on_init at
all. It is only helpful when starting Apache, to allow it to connect on
startup instead of at the first request wanting a db connection. But
it's not likely to give you a huge boost for that first request, since
setting up the DB connection (especially to localhost) isn't all that
resource-intensive. If you really do plan on having lots of databases,
I'd let Apache manage the initial connections to each database for you.
(Who knows--maybe a particular httpd child will never connect to half those
databases; if you use connect_on_init you've wasted those connections.)

Third, if you still insist on using connect_on_init with all of your
databases, you will need to execute connect_on_init for each database
you want Apache to connect to on startup. However you get that list of
databases is your business (hardcoded, SHOW DATABASES, whatever).

--keith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top