How to configure 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 batch size of objects to be inserted into db is about 40,000 objects. So far, we have used Spring.Net and NHibernate for our development, and I would like to be as consistent as possible and use the same technologies for mass insertion. I have experience with NHibernate, and I know that using a session without statuses will be an opportunity.

Is there a way to use Springs.Net transaction and session management, but using a session without NHibernate state? Using a Stateful session is not an option with so many objects, and I really want to use NHibernate rather than Spring.Net ADO

+4
source share
4 answers

You can create your own HibernateTransactionManager , which creates both types of sessions. This is a bit of an overhead since sessions are cheap to create.

Start by copying this class from the Spring.NET source tree. Open and close IStatelessSession when this is done for a regular ISession. Put your IStatelessSession in Spring.Threading.HybridContextStorage for easy access.

Next, create a GetStatelessSession extension method or method for your classes that require IStatelessSession.

Alternatively, and if you are using SQL Server, it is best to use SqlBulkCopy .

+3
source

You can use the OpenStatelessSession method in a factory session.

 public class Foo : HibernateDaoSupport { public void Bar() { using (var session = SessionFactory.OpenStatelessSession()) { // do something with the stateless session } } } 
+2
source

Transactions work the same way in both ISession and IStatelessSession. I do not know what Spring.Net does to support NHibernate, but if IStatelessSession is not yet supported, it should not be difficult to implement.

0
source
0
source

Source: https://habr.com/ru/post/1301681/


All Articles