views:

504

answers:

4

Hello,

I am currently developing an application that reads a bunch of data from a file. The usual size of the batch of objects to be inserted in the db is around 40.000 objects. So far we have used Spring.Net and NHibernate for our development and I'd like to be as consistent as possible and use the same technologies for the bulk insert. I've got experience with NHibernate and I know that using a Stateless session would be a possibility.

Is there a way to use Springs.Net transaction and Session management but using an NHibernate stateless session? Using a Stateful session is not an option with such a huge amount of object and I will really want to use NHibernate rather than Spring.Net ADO

A: 

Transactions work in the same way in both ISession and IStatelessSession. I don't know what Spring.Net does to support NHibernate, but if IStatelessSession isn't already supported it shouldn't be hard to implement.

James L
I know what you mean, my question is more related on how to tell Spring.net to use the IStatelessSession instead of the ISession :(
mandel
A: 

You could use the OpenStatelessSession method on the session factory.

public class Foo : HibernateDaoSupport
{
    public void Bar()
    {
        using (var session = SessionFactory.OpenStatelessSession())
        {
            // do something with the stateless session
        }
    }
}
Darin Dimitrov
I do know that that would be a way to go, but I'm using Spring.Net for the transaction management which also manages the session therefore I would like to have a way to tell the Spring.Net transaction management infrastructure to use a stateless sessions. (and yes, I know that ISession and IStatlessSessions are completely different)
mandel
Unfortunately `Spring.Data.NHibernate.HibernateAccessor` exposes only an `ISession` property which is used by `HibernateTemplate.Execute` method. You could probably try to write your own implementation of a `HibernateAccessor` but this seems like a lot of work. This work gets even more complicated from the fact that `ISession` and `ISatelessSession` interfaces are not compatible.
Darin Dimitrov
+1  A: 

You could build your own HibernateTransactionManager which creates both types of sessions. This has little overhead, as Sessions are cheap to create.

Start with a copy of that class from the Spring.NET source tree. Have it open and close a IStatelessSession when it does so for a regular ISession. Put your IStatelessSession into a Spring.Threading.HybridContextStorage for easy access.

Then create a method or extension method GetStatelessSession on your classes that need the IStatelessSession.

Alternatively, and if you are using SQL Server, you might be better served by using SqlBulkCopy.

Lachlan Roche
greatm that is what I was lookign for. Now, I'd love to give you the reward but it seems I cannot select the answer as the correct one :(
mandel
A: 

http://nhforge.org/blogs/nhibernate/archive/2008/10/30/bulk-data-operations-with-nhibernate-s-stateless-sessions.aspx - this link probably might be helpful. Too late, though, but might be useful for the others.

BreakPhreak