Thursday 12 February 2015

Testing with NHibernate and an in-memory SQLite database

Since reading this post, I've been using SQLite to test how my code uses NHibernate. This works perfectly for most situations. However, there are situations where you need multiple sessions. Opening another session with the config outlined in Ayende's post doesn't fail but you get a connection to another instance of a SQLite database, so it fails once you try and do something meaningful.

I've just found a way around this by using this connection string:
 FullUri=file:memory.db?mode=memory&cache=shared  

This creates an in-memory database that's shared amongst all connections with the same connection string, providing at least one connection is kept open. To the best of my knowledge the following config setting is still necessary:
 <property name="connection.release_mode">on_close</property>