How to bulk insert data with Nhibernate Stateless Session ?

With following code we can use NHibernate in bulk data operations, an area where ORM's traditionally perform pretty badly. To deal this, we use the nhibernate StatelessSession.
"A IStatelessSession has no persistence context associated with it and does not provide many of the higher-level life cycle semantics. In particular, a stateless session does not implement a first-level cache nor interact with any second-level or query cache. It does not implement transactional write-behind or automatic dirty checking."

using (IStatelessSession statelessSession = sessionFactoryHelper.OpenStatelessSession())
{
using (ITransaction transaction = statelessSession.BeginTransaction())
{
try
{
statelessSession.Insert(objEntity);
transaction.Commit();
return objEntity;
}
catch (Exception ex)
{
transaction.Rollback();
throw ex;
}
}
}

No comments:

Post a Comment