Search code examples
nhibernatepostgresqlnpgsql

NHibernate ISessionFactory.OpenSession() does not open a database connection


I have NHibernate configured with Fluent NNibernate connecting to a PostgreSQL database.

I have a worker class that takes an ISessionFactory as a constructor parameter and consumes messages from a queue. For each message the worker process calls ISessionFactory.OpenSession() and it does some database processing.

When I add more worker processes the performance of the system remains the same which is odd. After some more investigation I realized that all worker processes are using a single database connection. For example I would add 8 worker processes but on the database I can see only one database connection.

My understanding is that ISessionFactory.OpenSession() will open a new database connection unless the Connection Pool is full.

So is my understanding wrong or is this and issue with the Postgres NHibernate driver.


Solution

  • OpenSession does not open a database connection until needed, and it closes it (i.e. releases it back into the pool) as soon as possible.