Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,026 | Comments: 44,842

filter by tags archive

What is the cost of opening a session?

time to read 2 min | 300 words

Damien Guard asked me an interesting question:

Is SessionFactory.OpenSession() very lightweight because you call it on every request even if you have pages that don't need it.
I would have thought the lazy construction in CurrentSession get would have been a little safer.

The answer is that it is literally not worth your time to write the lazy construction. Here is what is happening when you opening a session:

public ISession OpenSession(IDbConnection connection, bool flushBeforeCompletionEnabled, bool autoCloseSessionEnabled,
							ConnectionReleaseMode connectionReleaseMode)
		new SessionImpl(connection, this, true, settings.CacheProvider.NextTimestamp(), interceptor,
						settings.DefaultEntityMode, flushBeforeCompletionEnabled, autoCloseSessionEnabled,

We new up a SessionImpl, which in turn:

internal SessionImpl(
	IDbConnection connection,
	SessionFactoryImpl factory,
	bool autoclose,
	long timestamp,
	IInterceptor interceptor,
	EntityMode entityMode,
	bool flushBeforeCompletionEnabled,
	bool autoCloseSessionEnabled,
	ConnectionReleaseMode connectionReleaseMode)
	: base(factory)
	using (new SessionIdLoggingContext(SessionId))
		if (interceptor == null)
			throw new AssertionFailure("The interceptor can not be null.");

		rootSession = null;
		this.timestamp = timestamp;
		this.entityMode = entityMode;
		this.interceptor = interceptor;
		listeners = factory.EventListeners;
		actionQueue = new ActionQueue(this);
		persistenceContext = new StatefulPersistenceContext(this);
		this.flushBeforeCompletionEnabled = flushBeforeCompletionEnabled;
		this.autoCloseSessionEnabled = autoCloseSessionEnabled;
		this.connectionReleaseMode = connectionReleaseMode;
		connectionManager = new ConnectionManager(this, connection, connectionReleaseMode, interceptor);

		if (factory.Statistics.IsStatisticsEnabled)

		if (log.IsDebugEnabled)
			log.Debug(string.Format("[session-id={0}] opened session at timestamp:{1}", SessionId, timestamp));


Is going to setup its fields and new up some additional stuff as well. All the work that is being done is merely newing up a few objects, and that is lightning fast. There is no database involved, NHibernate will manage the database connection to ensure minimal time spent with an open database connection automatically.


Jan Willem B

For me the reason to do lazy session initialization is to get a cleaner view in nhprof. If there are a lot of requests (images etc), nhprof can be quite cluttered with empty sessions.

Ayende Rahien


NH Prof is going to have an option to ignore empty sessions soon

Comment preview

Comments have been closed on this topic.


No future posts left, oh my!


  1. Technical observations from my wife (3):
    13 Nov 2015 - Production issues
  2. Production postmortem (13):
    13 Nov 2015 - The case of the “it is slow on that machine (only)”
  3. Speaking (5):
    09 Nov 2015 - Community talk in Kiev, Ukraine–What does it take to be a good developer
  4. Find the bug (5):
    11 Sep 2015 - The concurrent memory buster
  5. Buffer allocation strategies (3):
    09 Sep 2015 - Bad usage patterns
View all series


Main feed Feed Stats
Comments feed   Comments Feed Stats