[Dancer-users] Using persistent DB connections in Dancer::Tutorial

Gurunandan Bhat gbhat at pobox.com
Sat Jan 8 19:31:32 CET 2011

On Sat, Jan 8, 2011 at 8:26 PM, Puneet Kishor <punk.kish at gmail.com> wrote:

> Gurunandan Bhat wrote:
>> If I rewrite connect_db as:
>> my $dbh;
>> sub connect_db {
>>     if (! defined $dbh) {
>>         $dbh = DBI->connect("dbi:SQLite:dbname=".setting('database')) or
>>             die $DBI::errstr;
>>     }
>>     return $dbh;
>> }
>> will the application use a single connection for all connections under
>> all perl webservers (in-built, mod_perl, Starman, Twiggy etc.).
> I am interested in a definitive and clear answer as well. I tried the above
> method a while back, and kept on getting errors on transactions (I am using
> SQLite).
Admittedly, this (using a common database handle for all connections) might
not neccesarily be a Good Thing precisely because of transaction issues (and
consequently slowdowns because of locks) that it leads to.

However one way to handle transaction issues could be to have a per-session
database handle. Requestor passes a session id to connect_db, which sees if
the session has been allocated a handle and generates a new handle if not.
If the session is an old one, it merely picks up the correct previously
allocated  handle and returns it. If per-session persistence is possible,
then you should not have transaction issues.

Is that possible?

> Since I am using Apache, I added `PerlModule Apache::DBI` to my httpd.conf,
> and went back to a simple $dbh creation instead of checking for `defined
> $dbh`
> Knowing the correct way to implement this, perhaps even the way*s* for
> different servers (I am assuming different web servers might have different
> ways of implementing persistent connections) would be tremendously helpful.
>  If that is true, a line to that effect in an appropriate place in the
>> tutorial would emphasize the persistent features of the solution.
>> Also, are any hooks run when the application instance is destroyed? If
>> there are, implementing a graceful closing of open DB connection(s)
>> might provide a valuable lesson in the Tutorial.
>> Thank you.
>> _______________________________________________
>> Dancer-users mailing list
>> Dancer-users at perldancer.org
>> http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
> --
> Puneet Kishor http://punkish.org
> Carbon Model http://carbonmodel.org
> Charter Member, Open Source Geospatial Foundation http://www.osgeo.org
> Science Fellow
> http://creativecommons.org/about/people/fellows#puneetkishor
> Nelson Institute, UW-Madison http://www.nelson.wisc.edu
> ---------------------------------------------------------------------------
> Assertions are politics; backing up assertions with evidence is science
> ===========================================================================
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.backup-manager.org/pipermail/dancer-users/attachments/20110109/97cad01b/attachment.htm>

More information about the Dancer-users mailing list