You are viewing a plain text version of this content. The canonical link for it is here.
Posted to asp@perl.apache.org by k_berov <k_...@yahoo.com> on 2004/04/26 16:13:36 UTC
Persistent MySQL Connection????
Hi all.
Could anybody tell me how to setup and handle persistent connections
using DBI::mysql?
I am not allowed to use Apache::DBI.
I tried:
sub Application_OnStart{
$dbh = DBI->connect(
"DBI:mysql:database=mydatabase","me","mypassword",
{
PrintError => 1, # warn( ) on errors
RaiseError => 0, # don't die on error
AutoCommit => 1, # commit executes immediately
}
)|| sub{$DBI::errstr;undef $dbh};
}
#and then
sub Application_OnEnd{
sub{if($dbh){$dbh->disconnect} };
}
#A chek
sub check_connection_and_connect_if_needed{
if (!$dbh){
$dbh = DBI->connect(
"DBI:mysql:database=f8","f8","qazwsx",
{
PrintError => 1, # warn( ) on errors
RaiseError => 0, # don't die on error
AutoCommit => 1, # commit executes immediately
}
)|| sub{$DBI::errstr;undef $dbh};
}
}
sub Script_OnStart{
check_connection_and_connect_if_needed();
#.....
}
Where to put
$Server->RegisterCleanup(sub{if($dbh){$dbh->disconnect} });
if the server is restarted or killed
Is there a standart way
or som tweaking?
Thank in advance.
---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org
Re: Persistent MySQL Connection????
Posted by Josh Chamas <jo...@chamas.com>.
k_berov wrote:
> Thak you very much!!!
> So, if I understood clearly, I have to never disconnect.
>
> But doesn't it mean that i will leave an opened connection
> to MySql when Apache is stopped or my application crashes?
> My application is just one of the many on the ISP where I am hosted.
> Or I am $dbh->ping-ing the same connection?
>
Doing a disconnect() would be nice of you, since that would free up
one MySQL server side connection thread for other customers, but you would
have a slight performance benefit from not doing the disconnect
since you would not have to connect on each request if your site
is very busy. In a shared environment, I would not do the persistent
connection & I would disconnect, as you would be a good citizen then.
When Apache is stopped or crashes, a persistent MySQL connection
will automatically end with the apache/mod_perl process that
was connected to it.
Regards,
Josh
________________________________________________________________________
Josh Chamas, Founder | NodeWorks - http://www.nodeworks.com
Chamas Enterprises Inc. | NodeWorks Directory - http://dir.nodeworks.com
http://www.chamas.com | Apache::ASP - http://www.apache-asp.org
---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org
Re: Persistent MySQL Connection????
Posted by k_berov <k_...@yahoo.com>.
Thak you very much!!!
So, if I understood clearly, I have to never disconnect.
But doesn't it mean that i will leave an opened connection
to MySql when Apache is stopped or my application crashes?
My application is just one of the many on the ISP where I am hosted.
Or I am $dbh->ping-ing the same connection?
Thanks!!!
--- In apache-asp@yahoogroups.com, Josh Chamas <jo...@c...> wrote:
> k_berov wrote:
> > Hi all.
> > Could anybody tell me how to setup and handle persistent connections
> > using DBI::mysql?
> > I am not allowed to use Apache::DBI.
> > I tried:
> >
> > sub Application_OnStart{
> > $dbh = DBI->connect(
> > "DBI:mysql:database=mydatabase","me","mypassword",
> > {
> > PrintError => 1, # warn( ) on errors
> > RaiseError => 0, # don't die on error
> > AutoCommit => 1, # commit executes immediately
> > }
> >
> > )|| sub{$DBI::errstr;undef $dbh};
> > }
> >
>
> Use Script_OnStart, which gets run each script request, since you need
> the database connection initilized per process. A simple method
might look like:
>
> use vars qw($dbh);
> sub Script_OnStart {
> unless($dbh && eval { $dbh->ping }) {
> $dbh = DBI->connect(...);
> }
> }
>
> >
> > Where to put
> >
> > $Server->RegisterCleanup(sub{if($dbh){$dbh->disconnect} });
> > if the server is restarted or killed
> >
>
> Well, if you want the connect to be "persistent", then don't do
> this anywhere. But if you want the connection disconnected each
> request, then you can do this just after the unless{} block above
> in the Script_OnStart, but then you can get rid of the unless block
> altogether, and just have the DBI->connect part since you are not
> doing persistent connections.
>
> > Is there a standart way
> > or som tweaking?
> >
>
> I would say that using Apache::DBI is standard, but this is fine too.
>
> Regards,
>
> Josh
>
> ________________________________________________________________________
> Josh Chamas, Founder | NodeWorks - http://www.nodeworks.com
> Chamas Enterprises Inc. | NodeWorks Directory - http://dir.nodeworks.com
> http://www.chamas.com | Apache::ASP - http://www.apache-asp.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: asp-unsubscribe@p...
> For additional commands, e-mail: asp-help@p...
---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org
Re: Persistent MySQL Connection????
Posted by Josh Chamas <jo...@chamas.com>.
k_berov wrote:
> Hi all.
> Could anybody tell me how to setup and handle persistent connections
> using DBI::mysql?
> I am not allowed to use Apache::DBI.
> I tried:
>
> sub Application_OnStart{
> $dbh = DBI->connect(
> "DBI:mysql:database=mydatabase","me","mypassword",
> {
> PrintError => 1, # warn( ) on errors
> RaiseError => 0, # don't die on error
> AutoCommit => 1, # commit executes immediately
> }
>
> )|| sub{$DBI::errstr;undef $dbh};
> }
>
Use Script_OnStart, which gets run each script request, since you need
the database connection initilized per process. A simple method might look like:
use vars qw($dbh);
sub Script_OnStart {
unless($dbh && eval { $dbh->ping }) {
$dbh = DBI->connect(...);
}
}
>
> Where to put
>
> $Server->RegisterCleanup(sub{if($dbh){$dbh->disconnect} });
> if the server is restarted or killed
>
Well, if you want the connect to be "persistent", then don't do
this anywhere. But if you want the connection disconnected each
request, then you can do this just after the unless{} block above
in the Script_OnStart, but then you can get rid of the unless block
altogether, and just have the DBI->connect part since you are not
doing persistent connections.
> Is there a standart way
> or som tweaking?
>
I would say that using Apache::DBI is standard, but this is fine too.
Regards,
Josh
________________________________________________________________________
Josh Chamas, Founder | NodeWorks - http://www.nodeworks.com
Chamas Enterprises Inc. | NodeWorks Directory - http://dir.nodeworks.com
http://www.chamas.com | Apache::ASP - http://www.apache-asp.org
---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org