[dancer-users] how to make sure one db connection per user

Maxwell Carey mcarey at ucar.edu
Fri Sep 18 17:39:21 BST 2015


On Fri, Sep 18, 2015 at 10:32 AM, WK <wanradt at gmail.com> wrote:

> Hi!
>
> 2015-09-18 19:09 GMT+03:00 Dancer New <dancerfan2015 at gmail.com>:
>
> > I use Dancer::Plugin::Database in my application. Each time when
> > $dbh = DBI->connect( $c->{ dsn }, $c->{ username }, $c->{ password},
> $c->{
> > dbi_params })
> > is called. does it return a new connection?
>
> Yes, it does. But then is no point to use Dancer::Plugin::Database at all.
>

To expand on this a little, with Dancer::Plugin::Database, you should be
using the 'database' function instead of calling DBI->connect. See the
documentation here
https://metacpan.org/pod/Dancer::Plugin::Database#GETTING-A-DATABASE-HANDLE

"Calling database will return a connected database handle; the first time
it is called, the plugin will establish a connection to the database, and
return a reference to the DBI object. On subsequent calls, the same DBI
connection object will be returned, unless it has been found to be no
longer usable (the connection has gone away), in which case a fresh
connection will be obtained."



>
> > I would like one db connection for each user, where can I save the
> database
> > handler?
>
> Could you elaborate, why every user must have their own connection? It
> does not seem reasonable. Using just one connection over the
> application is much cheaper way. When using Dancer::Plugin::Database
> you must set up connection data in config file.
>
>
You can define multiple connections so that database('foo') will connect
with one set of connection parameters and database('bar') will use another.
See
https://metacpan.org/pod/Dancer::Plugin::Database#DEFINING-MULTIPLE-CONNECTIONS

But I agree, this seems like a bad idea. What exactly are you trying to do?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20150918/7c439615/attachment.html>


More information about the dancer-users mailing list