开发者

Closing SQL connections (NHibernate)

开发者 https://www.devze.com 2022-12-18 17:00 出处:网络
I have a test console app that is executing sql scripts to cre开发者_StackOverflow中文版ate a database and its tables, then inserting data. I use DAO to create, retrieve, update, and delete from the t

I have a test console app that is executing sql scripts to cre开发者_StackOverflow中文版ate 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;


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 ...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号