[web-devel] Database connection laziness (yesod)

Felipe Almeida Lessa felipe.lessa at gmail.com
Wed May 18 02:25:16 CEST 2011

On Tue, May 17, 2011 at 7:32 PM, Jeremy Hughes <jedahu at gmail.com> wrote:
> I ask because it would be neat to be able to write:
>> runDB $ getFromCache cache key
> where getFromCache queries the DB only on a cache miss, and have a
> connection created only in the case of a cache miss.

IIRC, runDB runs on a pool of DB connections, so it never creates a
connection, it only grabs one connection from the pool.  That said,
runDB needs to get a connection from the pool even if you don't use it
all.  Also, it needs to start and commit a transaction (although I
guess that drivers may optimize away the commit knowing that you
didn't do anything).  So while a connection startup/teardown cycle
won't be needed, you will need to use the resource that other thread
could be using.

Does that make sense?  I'm not sure if all I've said is correct, but I think so.

Cheers, =)


More information about the web-devel mailing list