6.8.3: Context-related sessions

Source: Internet
Author: User

All previous hibernate applications use the hibernateutil tool class when using sessions, this tool ensures that the thread-insecure session binding is restricted to the current thread ------ that is, implementing a context-related session.

Public class hibernateutil {public static final sessionfactory; static {try {// use the default hibernate. cfg. XML to start a configuration instance configuration = new configuration (). configure (); // create a sessionfactory instance sessionfactory = configuration by using the configuration instance. buildsessionfactory ();} catch (throwable ex) {system. err. println ("Initial sessionfactory creation failed. "+ ex); throw new exceptioninini Tializererror (Ex) ;}// threadlocal can isolate data sharing among multiple threads, therefore, you no longer need to synchronize public static final threadlocal <session> session = new threadlocal <session> (); public static session currentsession () throws hibernateexception {session S = session. get (); // If the thread does not have a session, create a new sessionif (S = NULL) {S = sessionfactory. opensession (); // store the obtained session variables in the threadlocal variable session. set (s);} return s;} public static void closesessio N () throws hibernateexception {session S = session. Get (); If (s! = NULL) S. Close (); Session. Set (null );}}

SlaveHibernate3At first, Hibernate addedSessionfactory. getcurrentsession ()Method, which can directly obtain context-related sessions. The early implementation of context-related sessions must depend on JTA transactions, so it is more suitable for the use of hibernate in containers.

From hibernate3.1, the underlying implementation of sessionfactory. getcurrentsession () is pluggable. hibernate introducesCurrentsessioncontext InterfaceAnd passHibernate. current_session_context_classParameters to manage the underlying implementation of context-related sessions.

The currentsessioncontext interface has the following three implementation classes:

① Org. hibernate. Context. jtasessioncontext:Trace and define context-related sessions based on JTA, which is exactly the same as the earliest method that only supports JTA.

② Org. hibernate. Context. threadlocalsessioncontext:Trace and define context-related sessions through the thread being executed, that is, it is similar to the session maintenance mode of hibernateutil above.

③ Org. hibernate. Context. managedsessioncontext:Trace and define context-related sessions through the thread currently being executed. However, the program needs to use the static method of this class to bind the session instance and unbind it. It does not automatically open, flush, or close any session.

If you useThreadlocalsessioncontextPolicy. The Hibernate session will be automatically opened with the getcurrentsession () method and closed with the transaction commit, which is very convenient.

For scenarios where Hibernate is used in containers, the first method is usually used.

For independent hibernate applications, the second method is usually used.

To specify the session management method used by hibernate, add the following fragments to the hibernate. cfg. xml file:

<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.current_session_context_class">jta</property>

For the third uncommon session management mechanism, you can simply write it as managed in the configuration file. For instance programs that use the current thread to define context-related sessions, refer to currentsession application in the light-weight javaee Enterprise Application Practice path of codes \ 06 \ 6.8.

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.