[Dancer-users] Dancer::Session::SQLite

David Precious davidp at preshweb.co.uk
Tue Aug 24 19:31:30 CEST 2010


On Tuesday 24 August 2010 16:52:10 P Kishor wrote:
> 1. Getting db settings. Initially, I thought I could use
> Dancer::Plugin::Database, so the same method could be used in the main
> Dancer application as well as in my session plugin. However, I have
> the following problems with D::P::Database -- (1) it doesn't really
> work in D::Session::SQLite. I get the error that database method is
> not declared (or something like that); 

With current Dancer versions, keywords defined by a plugin are exported only 
to the Dancer app which loaded the plugin, not to other plugins (or other 
scripts loaded by the app).  This is an annoying bug, and one which I hope to 
fix soon, if it hasn't been sorted already (I've been on holiday for two 
weeks, so don't yet know what's changed while I was away).

> and (2) it changes the regular,
> more familiar DBI methods. In fact, I am not even sure how I would do
> more DBI-ish things with it, such as transactions, etc. So, this is a
> different issue, but for now, I am tabling Dancer::Plugin::Database.

Actually, it doesn't change anything about DBI other than taking away the work 
of connecting to the database.

Calling the database() keyword simply returns a connected, ready to use DBI 
connection handle.  It may have been freshly connected, or it may be re-using 
the existing connection, but either way, you'll get a DBI connection handle to 
use as normal, so you might write something like:

  my $sth = database()->prepare('select * from table....');
  $sth->execute();
  ....

This is all documented in the documentation for D::P::Database.

I'm considering adding a few utility methods to D::P::Database to simplify 
some operations, but it will never prevent you from doing things in the usual 
DBI way, other than the initial connection to the database, which is what the 
plugin is designed to simplify.


> 2. This is more of a show-stopper. I am getting a lock on my database
> everytime a new session is inserted. 

That's to be expected with SQLite; the entire database is contained in a 
single file, which is locked when you connect to it.

> So, I am trying to figure out where to disconnect the db manually, but
> I don't really understand the sequence of actions that Dancer::Session
> takes. I have the following methods in my module
[...]

In order to avoid it being locked, you'd probably need to disconnect in 
flush() after storing the session data, and probably in retrieve() right after  
fetching the session data too.

This could result in many repeated connection & disconnections, but may be the 
only way to reliably use SQLite without incurring locking problems.

Cheers

Dave P


-- 
David Precious <davidp at preshweb.co.uk>
http://blog.preshweb.co.uk/    www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedin    www.preshweb.co.uk/facebook
www.preshweb.co.uk/identica    www.lyricsbadger.co.uk

  "Programming is like sex. One mistake and you have to support 
  it for the rest of your life". (Michael Sinz)


More information about the Dancer-users mailing list