Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

persistent dbi handles

3 views
Skip to first unread message

Peter Kopke Jr.

unread,
Oct 20, 2009, 3:28:49 PM10/20/09
to dbi-...@perl.org
Hello,

I have a web-based system that accesses a mysql database through DBI.
Currently I make a new database connection for each web access. I
would like to retain the connection and database handle rather than
making a new connection with each web access. Is there a simple way
to do this?

Thanks,
Peter

John Scoles

unread,
Oct 21, 2009, 7:10:36 AM10/21/09
to Peter Kopke Jr., dbi-...@perl.org
Yes there are a number of ways to do this.

If you are using apache web server they simply do

use Apache::DBI;

rather than

use DBI;

and it will let your reuse session and connections for you


or a longer term approach could be found with

http://search.cpan.org/~mws/ResourcePool-1.0104/lib/ResourcePool.pod

Alexander Foken

unread,
Oct 21, 2009, 11:53:36 AM10/21/09
to sco...@pythian.com, Peter Kopke Jr., dbi-...@perl.org
John Scoles wrote:
> Yes there are a number of ways to do this.
> If you are using apache web server they simply do
>
> use Apache::DBI;

Hmm, that helps with mod_perl, but not with CGIs (which are still
commonly used with Apache). FastCGI / mod_fastcgi can also profit from
persistent connections, but you have to manage them yourself.

Alexander

Ron Savage

unread,
Oct 21, 2009, 6:02:18 PM10/21/09
to List DBI
Hi Alexander

Hmm, has anybody tried Apache::BabyConnect?

I did a search on CPAN for 'persistent'.
--
Ron Savage
r...@savage.net.au
http://savage.net.au/index.html


Bill Moseley

unread,
Oct 22, 2009, 12:18:42 AM10/22/09
to Peter Kopke Jr., dbi-...@perl.org

Something like this?

my $dbh;

# later

sub request {

$dbh ||= DBI->connect_cached( @args, { private_pid => $$ } );

Or better would be to test if $$ != $saved_dbh_pid and set InactiveDestroy
on the old $dbh and then create a new $dbh.

Then wrap the request in eval block and rollback on errors.

Or maybe try this:

http://search.cpan.org/~dwheeler/DBIx-Connector-0.20/lib/DBIx/Connector.pm


--
Bill Moseley
mos...@hank.org

0 new messages