Skip to content

DRNJ

Light at the end of the Technology Tunnel

  • Home
  • About
  • Contact
DRNJ

NHibernate Connection Cycling

The Problem

On a webAPI (C#/.Net 4) using NHibernate 4 an issue was observed under stress testing. The issue was associated with the user-impersonation being done on the database connection – the impersonation would work correctly, however, for subsequent database operations would fail. After much (and I do mean much) investigation it was found that the connections seem to be “cycled” amongst the NHibernate sessions – so we could observe the session using a connection with one particular HashCode (unique object identifier) and see the user-impersonation SQL execute, but when it came to some ORM write-backs to the DB another connection with another user’s impersonation (and differing HashCode) was observed in the session for the db operation.

Weird or what ?

The Solution

I must give a round of applause to the guys at StackOverflow for this one.

Basically, create a dummy session from the session factory , then pass the connection of this session to the OpenSession method of the session factory to create the session you’re going to use.

Simples

ISession dummy = sessionFactory.Factory.OpenSession();
this.Session = sessionFactory.Factory.OpenSession(dummy.Connection);

 

DotNet, WebAPI

Leave a Reply Cancel reply

You must be logged in to post a comment.

Idealist by NewMediaThemes