mandel
mandel

Reputation: 2947

How do you set up NHibernate to use a Stateless session using Spring.Net?

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

Upvotes: 4

Views: 6372

Answers (4)

Alexander Abramovich
Alexander Abramovich

Reputation: 11438

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.

Upvotes: 0

Lachlan Roche
Lachlan Roche

Reputation: 25956

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.

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1038890

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
        }
    }
}

Upvotes: 2

James L
James L

Reputation: 16864

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.

Upvotes: 0

Related Questions