Yavor S
Yavor S

Reputation: 1493

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.

Upvotes: 2

Views: 1770

Answers (2)

Vadim
Vadim

Reputation: 17955

By default the session will keep the connection open for the life time of a transaction and as Diego said, it only opens it when needed.

If you want to manage your own connections you can call

ISessionFactory.OpenSession(myConnection);

Upvotes: 3

Diego Mijelshon
Diego Mijelshon

Reputation: 52745

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.

Upvotes: 3

Related Questions