The basic feature of NHibernate is to accomplish the mapping of object-oriented programming language to relational database, and to use the Persisted object Po (persistent object) to complete the persistence operation in NHibernate. The operation of the PO must be in Session management to synchronize to the database, but here the session does not mean httpsession, it can be understood that the connnection,session based on ADO is the center of NHibernate operation, Object life cycle, transaction management, database access are closely related to the session, first of all, we need to know that Sessionfactory is responsible for creating session,sessionfactory is thread-safe, Multiple concurrent threads can access a sessionfactory and get a session instance from it at the same time. only one data storage source (database), just create a sessionfactory,sessionfactory is a heavyweight object, if the application has only one data storage source, just create a sessionfactory instance, Because creating sessionfactory instances at will consumes a lot of memory space. What is said here is to create only one, usually using a singleton pattern, so that all requests in this application domain will have access to the same sessionfactory. But his example session is lightweight. The session is not thread-safe, meaning that if multiple threads use a session instance for data access at the same time, the session data access logic will be confused. Therefore, the session instance created must be run on the local access empty so that it is always related to the current thread. The session is not thread-safe, that is, if multiple threads use a session instance for data access at the same time, the session data access logic will be confused. Therefore, the session instance created must run on the local access empty so that it is always related to the current thread.
Multiple line approached (that is, multiple request) cannot use a session, but there may be multiple sessions in a request, not from a performance standpoint, and this will not be an error. Just like ADO, when you create two connection in a single method to operate the same database, you can do it despite performance problems. Note that if a transaction transaction is involved, it must be done in the same session.
In order to ensure a browser access, that is, a request, the use of the respective session, in Java (Hibernate), ThreadLocal is a special thread binding mechanism in Java, through ThreadLocal access to data, is always related to the current thread, that is, the JVM binds the private local instance access space for each running thread, thus providing a separate machine for concurrent access problems that often occur in multithreaded environments, threadlocal is not a thread-localized implementation, but a thread-local variable. That is, each thread that uses the variable must provide a copy of the variable, and each thread changes the value of the variable only to change the value of the copy, without affecting the value of the variable for the other thread, threadlocal is to isolate data shares for multiple threads, and there are no shared resources between multiple threads. It is no longer necessary to synchronize threads.
in ASP. NET MVC, it is usually used in conjunction with a controller. The service method that calls each domain in the controller eventually performs various operations on the database, so we just have to bind a new session to the current thread before the Controller's action is executed. In this request process, all operations on the database use this session.
AttributeUsageAttribute (AttributeTargets.Method, inherited = False, AllowMultiple = false)] public class Nhibernatesessionattribute:filterattribute, Iactionfilter {public nhibernatesessionattribute (): Base () { } public void OnActionExecuting (ActionExecutingContext filtercontext) { var session = Sessionprovider.getnewsession ();//Here is the acquisition of a new session. Because this is the design that was just started for each operation NHibernate Currentsessioncontext.bind ( session);//Open a new session, put it into the current context, and then use the session later Getcurrentsession can be } public void onactionexecuted (ActionExecutedContext filtercontext) { Currentsessioncontext.unbind ( Sessionprovider.getsessionfactory ()). Close (); }
When using, only need to add a nhibernatesession filter on the action, you can execute the filter before the action, bind the session, and then after the session is dismissed
[Nhibernatesession] Public ActionResult Saveneworder (order order) { ... Return redirecttoaction ("Index"); }
Sessionprovider User Management session acquisition is a static class that guarantees the uniqueness of sessionfactory, which is compared with performance considerations.
public class Sessionprovider {private static isessionfactory sessionfactory; public static NHibernate.Cfg.Configuration Configuration; Static Sessionprovider () {configuration = new NHibernate.Cfg.Configuration (). Configure (); sessionfactory = Configuration. Buildsessionfactory (); } public static Isessionfactory Getsessionfactory () {return sessionfactory; } public static ISession Getnewsession () {return sessionfactory.opensession ();//This method is generally used for the first time Sess Ion, and then bound to context} public static ISession getneworcurrentsession () {ISession session = N Ull if (! Currentsessioncontext.hasbind (Sessionfactory)) {session = Sessionfactory.opensession (); Currentsessioncontext.bind (session); } else {session = Sessionfactory.getcurrentsession (); if (!session. IsOpen) session = Sessionfactory.opensession (); } return session; } }
Note: If we are building the Windwos service, we do not have the binding in action and the process of unbinding it. The session here is thread_static, so when the session is used, it will be taken directly to the session = Sessionprovider.getneworcurrentsession. Note, however, that there is not a process to unbind. in ASP. NET MVC, the Flush method is called automatically when Unbind, and then the database is updated. And if we do not manually call Session.flush in the Windows Service program to update the database, all operations are not synchronized to the database. When this thread ends, the session disappears and everything is not synchronized. So, we have to remember to call the Flush method manually.