In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to understand Hibernate Session management". In daily operation, I believe many people have doubts about how to understand Hibernate Session management. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand Hibernate Session management". Next, please follow the editor to study!
Hibernate Session is the center of Hibernate operation. The life cycle of objects, the management of transactions and the access of database are all closely related to Session. Just like the need to care about the management of Connection when writing JDBC, to create, utilize and recycle Connection in an effective way to reduce resource consumption and increase system execution efficiency, effective Session management is also the focus of Hibernate applications.
Hibernate Session is created by SessionFactory, SessionFactory is thread-safe (Thread-Safe), you can let multiple threads access SessionFactory at the same time without the problem of data sharing, but Session is not designed to be thread-safe, so trying to get multiple threads to share a Session will lead to confusion in data sharing.
In the Quick start chapter in the Hibernate reference manual, you demonstrate a HibernateUtil that uses the ThreadLocal category to create an auxiliary class for Session management, which is a widely used solution for Hibernate Session management, ThreadLocal is an operational example of the Thread-Specific Storage pattern, and you can learn about the Thread-Specific Storage pattern in the following article:
Designpattern:Thread-Specific Storage
Because Thread-Specific Stroage mode can effectively isolate the data used by threads, to avoid the problem of data sharing among multiple threads of Session, the HibernateUtil classes in the Hibernate reference manual are listed below:
HibernateUtil.java import net.sf.hibernate.*; import net.sf.hibernate.cfg.*; public class HibernateUtil {private static Log log = LogFactory.getLog (HibernateUtil.class); private static final SessionFactory sessionFactory; static {try {/ / Create the SessionFactory sessionFactory = new Configuration (). Configure (). BuildSessionFactory ();} catch (Throwable ex) {log.error ("Initial SessionFactory creation failed.", ex); throw new ExceptionInInitializerError (ex) } public static final ThreadLocal session = new ThreadLocal (); public static Session currentSession () throws HibernateException {Session s = (Session) session.get (); / / Open a new Session, if this Thread has none yet if (s = = null) {s = sessionFactory.openSession (); session.set (s);} return s;} public static void closeSession () throws HibernateException {Session s = (Session) session.get (); session.set (null) If (s! = null) s.close ();}}
The Session is temporarily saved in the same thread, but don't worry about the persistent occupation of the database connection Connection, Hibernate will only get the Connection (from the connection pool) when the database operation is really needed.
At this point, the study on "how to understand Hibernate Session management" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.