开发者

What is the difference between StatelessSession and Session in NHibernate?

开发者 https://www.devze.com 2023-03-08 09:42 出处:网络
开发者_运维百科What is the difference between StatelessSession and Session in NHibernate?Stateless session is not tracking entities that are retrieved. For example for regular ISession following code:
开发者_运维百科

What is the difference between StatelessSession and Session in NHibernate?


Stateless session is not tracking entities that are retrieved. For example for regular ISession following code:

var session = sessionFactory.OpenSession()
using(var transaction = session.BeginTransaction()){
    var user = session.Get<User>(1);
    user.Name = "changed name";
    transaction.Commit();
}

will result in update in DB. This tracking consumes memory and makes ISession performance to degrade over time since amount of tracked entities is growing.

The same code with IStatelessSession won't do anything. Stateless sessions are used when you need to load lots of data and perform some batching operations. It can be used to work with large data sets in a more "ado.net" style.


Session in the NHibernate caches all the inserted data in the session level cache.

 using (var session = sessionFact.OpenSession())
         {
            using (var trans = session.BeginTransaction())
            {
              for (int  = 0; i < 500000; i++)
              {
                 Student st = new Student(
                 {  
                      ID = 1,
                      FirstName = "Zia",
                      LastName = "Qammar"
                 });

                 session.Save(st);
               }

               trans.Commit();
            }
          }

above code throw an "OutOfMemoryException" exception while inserting 50,000 student in the database. The other approach that NHibernate provides is StatelessSession which persist the data in database in detached objects.

using (var session = sessionFact.OpenStatelessSession())
             {
                using (var trans = session.BeginTransaction())
                {
                  for (int  = 0; i < 500000; i++)
                  {
                     Student st = new Student(
                     {  
                          ID = 1,
                          FirstName = "Zia",
                          LastName = "Qammar"
                     });

                     session.Save(st);
                   }

                   trans.Commit();
                }
              }
0

精彩评论

暂无评论...
验证码 换一张
取 消