views:

195

answers:

1

I have a test console app that is executing sql scripts to create a database and its tables, then inserting data. I use DAO to create, retrieve, update, and delete from the tables and then try and drop the database, but it can't because it says it is currently in use. How do I kill the connection? Through debugging and running a sql script to see the total connections, I've narrowed it down to this piece of code that is not killing the connection. Even though it says the connections are cleared, the database says it is still connected.

foreach (ISessionFactory factory in this.connections.Values)
{
    factory.Close();
}

this.connections.Clear();
this.connections = null;
+1  A: 

NHibernate manages all the database connections itself. It opens and closes the database as it sees fit, but you can try to call Session.Disconnect to get it to disconnect from the ADO.NET connection.

However ... if you have Connection Pooling enabled, which is normally the 'default' connection behavior, the connection will only be returned to the connection pool, not released, and it will still show as a connection, so you would have to clear the connection pool.

Unfortunately, I do not know if you can do this directly thru NHibernate, but you can use a SQLConnection object to issue a ClearPool or ClearAllPools call ...

Douglas Troy
Thanks! I was originally clearing the pools in the code and it wasn't working so I tried using Pooling=false in the connection string and it seems to do the trick. Why it works in the connection string and not in the code beats me.
Sarah