less than 1 minute read

I spent the last month learning NHibernate and I am definitely a fan. I have been using SQLite for unit testing similar to Ayende in his post. The problem I ran into is that every time I ran my tests the connection was getting reset. A little google whispering and I found the answer in Justin Etheredge’s post. Hopefully this will help me remember as well as others.

Here is what the code should look like:

public override ISessionFactory BuildSessionFactory()
{
   var properties = new Dictionary
                        {
                            { "connection.driver_class", "NHibernate.Driver.SQLite20Driver" },
                            { "dialect", "NHibernate.Dialect.SQLiteDialect" },
                            { "connection.connection_string", "Data Source=:memory:;Version=3;New=True;" },
                            { "connection.provider", "NHibernate.Connection.DriverConnectionProvider" },
                            { "connection.release_mode", "on_close" },
                            { "show_sql", "true" }
                        };

    configuration = new Configuration { Properties = properties };
    configuration = configuration.AddAssembly(typeof(Ticket).Assembly);
    return configuration.BuildSessionFactory();
}

The winning line is “connection.release_mode” setting it to “on_close”.

Listened to: Apollo I: The Writing Writer from the album “From Fear Through The Eyes Of Madness” by Coheed & Cambria

Updated: