NHibernate using stateless sessions

Source: Internet
Author: User

NHibernate 3.0 Cookbook The third chapter, Using stateless sessions translation.

When dealing with large amounts of data, you may often use the more "bottom-up" API to improve performance, and many of the advanced features are turned off in this process. In NHibernate, the stateless session is the high performance, underlying API.

This article, we will use a stateless session to update our movie prices.

Get ready

As in the previous section, create a console application, refer to the second chapter using App. config to configure NHibernate, using Eg.core Model.

How to Do

1. Create some data and add the following code to your main method:

using (var session = Sessionfactory.  Openstatelesssession()) {    using (var tx = session). BeginTransaction ()) {for        (int i = 0; i <; i++)            session. Insert (new Movie () {                Name = "Movie" + i.tostring (),                Description = "A great movie!",                UnitPrice = 14.95M,                D Irector = "Johnny Smith"            });        Tx.commit ();    }}

2. Next, to update the price of our movie, add the following code to the Main method:

using (var session = Sessionfactory.  Openstatelesssession()) {    using (var tx = session). BeginTransaction ()) {        var movies = getmovies (session);        foreach (var movie in movies) {            updatemovieprice (movie);            Session. Update (movie);        }        Tx.commit ();    }}

3. Add the Getmovies method:

Static Ienumerable<movie> Getmovies (istatelesssession session) {    return session. CreateQuery ("from Movie")        . List<movie> ();}

4. Finally, add the Updatemovieprice method:

static random rnd = new Random (), static void Updatemovieprice (movie movie) {    //Random price between $9.95 and $24.95    movie. UnitPrice = (decimal) rnd. Next (Ten)-0.05M;}
Principle

Using a stateless session, we created 1000 new movies. A stateless session does not implement transactional write-behind, which means that the SQL statement is not deferred until we commit the transaction. However, When we open batches, they are not executed immediately. At the same time, 100 INSERT statements are queued and all statements are sent together. If we close the batch, then each session is called. Insert will send a statement immediately.

Next, we use a query statement to get all the movies. These films are free-form; they are not associated with a session. Entities are not associated with a stateless session. This is true whether we use a query or a GET method to load entities.

Since the stateless session does not implement an automatic dirty check (automatic dirty checking), we must call the session. Update to save changes to the movie.

There ' s more ...

A stateless session is essentially a simplified version of a standard session. It does not use a first-level cache, does not perform an automatic dirty check (automatic dirty checking), and does not support lazy loading. Actually, it doesn't even have a reference to the entity, To help prevent memory leaks when processing thousands of entities. Cascading is ignored. You must explicitly insert, update, or delete each entity each time. The stateless session also ignores level two caches, event snooping (listeners), interceptors (interceptors), Even Nhibernate.sql's log4net recorder.

Despite these limitations, stateless sessions are very useful in high-performance batch scenarios when you need to use real objects. When you want to use raw data, there are usually better alternatives, such as simple old SQL (plain Old SQL), HQL batch processing (HQL bulk actions), SqlBulkCopy, or ETL tools. As for the Simple legacy SQL (plain-SQL), session. The connection property makes it easy to use the ADO. Connection object, and then you can write the ADO code as before.

NHibernate using stateless sessions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.