<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Date: Tue, 25 Aug 2015 11:45:28 +0100<br>
From: Andrew Beverley <<a href="mailto:andy@andybev.com">andy@andybev.com</a>><br>
To: Perl Dancer users mailing list <<a href="mailto:dancer-users@dancer.pm">dancer-users@dancer.pm</a>><br>
Subject: Re: [dancer-users] database connection in dancer<br>
Message-ID: <<a href="mailto:1440499528.28975.7.camel@andybev.com">1440499528.28975.7.camel@andybev.com</a>><br>
Content-Type: text/plain; charset="UTF-8"<br>
<br>
On Tue, 2015-08-25 at 11:38 +0100, Dancer New wrote:<br>
> Hi,<br>
<br>
Hi,<br>
<br>
> I am trying to implement a user login on a website.<br>
><br>
> Each time a user logon, the session record the user name.<br>
<br>
Firstly, have you considered using an existing authentication module, such as<br>
Dancer2::Plugin::Auth::Extensible? This will save reinventing the wheel.<br>
<br></blockquote><div> I am using Dancer.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> The problem I have now was the database connection in 'before hook' (<br>
> setting user context) and the db connection in the route for actual query<br>
> seems two different db connections. So the database doesn't know they are<br>
> from the same user request and therefore doesn't return the right results.<br>
<br>
It sounds like you are initiating a new database connection for each page request?<br>
If so, that's very expensive, and not likely to be very scalable.<br></blockquote><div><br></div><div>I would like to know how to make one db connection and use it in all requests. where to store the database handler? </div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> How to keep the database connection in 'before hook' and pass the same<br>
> database connection to other route?<br>
<br>
If you still decide to do it this way, then you could save the database connection<br>
using var and then read that value later:<br>
<br>
var mydb => $db;<br>
...<br>
my $db = var 'mydb';<br>
<br>
Andy<br>
<br>
<br>
<br></blockquote></div></div></div>