NHibernate Sessions in WCF Singleton services
We are currently using NHibernate in our WCF service layer for all data access operations.
We have used the UnitOfWork pattern to set up the ISession based on the following [example][1]:
We have implemented a ThreadStatic ISession as described in the example.

We have also used the IDispatchMessageInspector to start and stop the UnitOfWork : 

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
     {
         UnitOfWork.Start();
         return null;
     }
    
     public void BeforeSendReply(ref Message reply, object correlationState)
     {
         UnitOfWork.Current.Dispose();
     }


Our WCF services have been implemented as Singletons :

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]

The issue with this approach is that :
1) A UnitOfWork does not get started unless there is a WCF request received.
So if I need to implement some sort of a polling service , I have to manually start and stop the UnitOfWork

2) The ISession is ThreadStatic ..so yes it is ThreadSafe and since we are using the Singleton mode I do understand that it needs to be ThreadStatic.

   But lets say if I need to parallelize a large insert operation ..where I need to create say 300000 objects and persist them in a rally short time ..then I again need to start and stop the UnitOfWork inside by Parallel.For loop so that every thread gets its own ISession.
This works fine .. but I'm unable to wrap this call in a single transaction as each thread
works on a separate session

Had my ISession not been ThreadStatic , I could pass the same ISession to each thread and 
have transactional integrity implement on the entire parallel operation.  I do understand that its not a good idea to have multiple threads using the same session ..but in this case I'm doing a bulk insert ..so can it be justified ?

So the questions that I have are:

What other options do we have to setup NHibernate sessions in WCF Singleton services besides the UnitOfWork pattern . 

How do I maintain a transaction across a parallel operation ( when using NHibernate) ?





  [1]: http://nhforge.org/wikis/patternsandpractices/nhibernate-and-the-unit-of-work-pattern.aspx
NHibernate Sessions in WCF Singleton services
===

We are currently using NHibernate in our WCF service layer for all data access operations.
We have used the UnitOfWork pattern to set up the ISession based on the following [example][1]:
We have implemented a ThreadStatic ISession as described in the example.

We have also used the IDispatchMessageInspector to start and stop the UnitOfWork : 

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
     {
         UnitOfWork.Start();
         return null;
     }
    
     public void BeforeSendReply(ref Message reply, object correlationState)
     {
         UnitOfWork.Current.Dispose();
     }


Our WCF services have been implemented as Singletons :

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]

The issue with this approach is that :
1) A UnitOfWork does not get started unless there is a WCF request received.
So if I need to implement some sort of a polling service , I have to manually start and stop the UnitOfWork

2) The ISession is ThreadStatic ..so yes it is ThreadSafe and since we are using the Singleton mode I do understand that it needs to be ThreadStatic.

   But lets say if I need to parallelize a large insert operation ..where I need to create say 300000 objects and persist them in a rally short time ..then I again need to start and stop the UnitOfWork inside by Parallel.For loop so that every thread gets its own ISession.
This works fine .. but I'm unable to wrap this call in a single transaction as each thread
works on a separate session

Had my ISession not been ThreadStatic , I could pass the same ISession to each thread and 
have transactional integrity implement on the entire parallel operation.  I do understand that its not a good idea to have multiple threads using the same session ..but in this case I'm doing a bulk insert ..so can it be justified ?

So the questions that I have are:

What other options do we have to setup NHibernate sessions in WCF Singleton services besides the UnitOfWork pattern . 

How do I maintain a transaction across a parallel operation ( when using NHibernate) ?





  [1]: http://nhforge.org/wikis/patternsandpractices/nhibernate-and-the-unit-of-work-pattern.aspx