Eatdoku
Eatdoku

Reputation: 6951

ISessionFactory.OpenSession() from multiple threads

I would like to know the behavior of the following.

basically i have a static ISessionFactory, and an application with 10 threads running and each of them would use ISessionFactory.OpenSession() to get an ISession. Would this cause any problem?

Upvotes: 4

Views: 2800

Answers (3)

Dmitry
Dmitry

Reputation: 17350

This will not cause any problems, but make sure that:

  • you don't 'leak' ISession instance (no other threads will ever have access to it)

  • you properly Dispose session when you no longer need it

ISessionFactory on the other hand is thread safe and can be used from multiple threads without additional synchronization on your part.

using(ISession session = _sessionFactory.OpenSession()) {
    // use session making sure it will not become visible to other threads
}

Upvotes: 1

Shuhel Ahmed
Shuhel Ahmed

Reputation: 963

SessionFactory is thread safe but not Session. So if you open a session with ISessionFactory.OpenSession() in a thread and use it there(within that thread) without sharing with other thread, you are safe to go.

But do not use ISessionFactory.GetCurrentSession() among multiple therads.

Upvotes: 1

csano
csano

Reputation: 13686

No. This is correct. You want to make sure you have a separate session for each thread.

Upvotes: 4

Related Questions