NHibernate mysql connection not closed?

I use NHibernate with mySQL 5, and I'm a little unsure if NHibernate really closes the connection to mySQL.

This is the code I use to store the Player object in the database:

using (ISession session = SessionFactory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { session.Save(player); transaction.Commit(); } } 

After running this piece of code, I select the current number of connections from the mySQL server using

 show status like 'Conn%' ; 

and the value increases each time by 1!

Thus, inserting a player 10 times (= starting the program 10 times) increases the number of connections by 10. But the NHibernate session.IsClosed property of the session is false.

Do I need to do something to free up NHibernate resources and close the connection? Are connections / timeout connected?

It would be great if someone could give me a hint.

+4
source share
2 answers

I am sure the connection is closing. The dispose method on ISession does just that. Therefore, it is unlikely that it will remain open when you exit the use block.

Also, from what I know, a connection usually opens only when writing / reading to the database. A session may be open, but this does not mean that the connection is also open.

This, plus the fact that the mysql documentation says that the Connect keyword means:

The number of connection attempts (successfully or not deleted) to the MySQL server.

and says nothing about the current state of the connection (open or not), makes me think that the connection is closed.

PS: you must use Threads_connected to see open connections.

+2
source

There is an error in the MySQL.Data files in some versions that causes me this problem. Make sure you update your Connector / NET to the latest. This solved the problem for me.

0
source

All Articles