There's a good way and a bad way.
First, the bad way. For generic, run-of-the-mill persistent variables, a
package level variable will do the trick. C::A and "my" variables will be
destroyed by the garbage collector when the script is done executing.
However, package level variables are around until the package is unloaded
(around when END blocks are executed)
Here's an example...
####################################################
package MyCgiApp ;
use base 'CGI::Application' ;
use DBI ;
our $persistent_var = 0 ; # persistent
sub cgiapp_init
{
$persistent_var++ ; # a running tally of how many times
# the script is called.
}
###################################################
Here's the problem with this for an SQL connection... Many bargain basement
web hosts will have SQL connections automatically die with an extremely
short timeout period. If this is the case, then the connection might die in
between one execution and the next. Therefore, it is important to check
that the database is alive before actually using it. For example, using the
$dbh->ping() method.
This, of course, is a royal pain in the kicker, and so we have much better
methods for you to use.
The DBI package has a built in function call for pooled database
connections. Simply calling DBI->connect_cached(...) instead of
DBI->connect(...) with the same exact parameters will get the cached
DBI->copy if
(a) it exists and (b) it is still alive. This would be preferred for
obvious reasons.
For more information on connect_cached(), see
http://search.cpan.org/~timb/DBI-1.48/DBI.pm
-Josh
--
Josh Danziger
[EMAIL PROTECTED]
-----Original Message-----
From: Sven Neuhaus [mailto:[EMAIL PROTECTED]
Sent: Monday, October 31, 2005 4:05 AM
To: [email protected]
Subject: [cgiapp] CAP::DBH + FastCGI: persistent DB connections, please
Hello,
when using CGI::Application::Plugin::DBH with FastCGI (or mod_perl), are the
database connections persistent? If not, what do I have to do to make them
persistent? It looks like the database handles are only stored in the
C::A-object, however that object is destroyed after every request.
What would be a good place to store them to make them persistent?
Thanks,
-Sven
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/[email protected]/
http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/[email protected]/
http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]