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 Spri开发者_运维知识库ng.Net ADO
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
.
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
}
}
}
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.
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.
精彩评论