we have a WCF webservice handling some business entities. ORM is nhibernate 4.0.4, .NET 4.0. We are using an IDispatchMessageInspector to open a session and a transaction in AfterReceiveRequest, context class is wcf_operation:
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
if (!CurrentSessionContext.HasBind(this.factory))
{
this.log.Log.Debug("Creating NH Session");
var session = this.factory.OpenSession();
session.FlushMode = FlushMode.Never;
session.BeginTransaction();
CurrentSessionContext.Bind(session);
}
return null;
}
The transaction is committed and the session is closed in BeforeSendReply. This is working as long as only one call at a time deals with a specific entity.
If two concurrent webservice try to update the same entity, I get an nhibernate exception
NHibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
As I understand that two updates collide on the database level, I do not understand the problem nhibernate has here. From my understanding, the two sessions of the two calls should be independent of each other; am I missing something here? A configuration maybe?
As said, I see the problem the database has; nevertheless, I would expect an exception claiming something about conccurent update or so. Since the traffic on the webservice grows, I fear that I have a general problem with my session handling.
BeforeSendReplymethod? - Andrew Shepherd