[dancer-users] Dancer + objects + database

Andrew Beverley andy at andybev.com
Fri May 1 14:53:47 BST 2015


On Fri, 2015-05-01 at 12:26 +0200, Juan José 'Peco' San Martín wrote:
> Currently, inside each Dancer route I'm forced to create the master object
> of the application (i.e $object=Myapp->new(-login=>$login)) that it's a
> huge structure, including the database handler.
> 
> So:
> /route1 => creates the $object, reconnects to the database
> /route2 => creates the $object, reconnects to the database
> 
> Yep, doesn't make sense...
> 
> Is there a way to make the $object persistent (and the database connection
> with it)?

For the database connection itself, you could use one of the database
connection plugins, which essentially just create a global handle that
is persistent.

I'd be interested to hear what people think about a more generic
persistent object though, as I have this problem myself. I've thought
about having it as a global variable outside of the routes, which would
probably work, but I'm concerned about it getting stale and/or being
wrongly built for the relevant user. Maybe putting it in a session
variable could be a good solution?

> Is this related to how the application is running (CGI)?

If you're running in plain CGI, then I think the entire dancer app is
fired-up for each request, so you want to at least run in fast-CGI (or
equivalent).

Andy




More information about the dancer-users mailing list