In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the use of Hibernate Session". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "what is the use of Hibernate Session" together!
Hibernate's basic feature is to complete the mapping of object-oriented programming languages to relational databases, using persistent objects PO in Hibernate (Persistent Object) Completes persistent operations. PO operations must be synchronized to the database under Session management. However, Session here does not refer to HttpSession. It can be understood as Connection based on JDBC. Session is the center of Hibernate operation. The life cycle of objects, transaction management, and database access are all closely related to Session. First, we need to know that SessionFactory is responsible for creating sessions, SessionFactory is thread-safe, multiple concurrent threads can access a SessionFactory at the same time and obtain Session instances from it. Session is not thread-safe, that is, if multiple threads use a Session instance for data access at the same time, it will lead to Session data access logic confusion. Therefore, the Session instance created must run on local access space so that it is always relevant to the current thread.
ThreadLocal is a special thread binding mechanism in Java. Data accessed through ThreadLocal is always related to the current thread. That is to say, JVM binds a private local instance access space for each running thread, thus providing an isolation mechanism for concurrent access problems that often occur in multithreaded environments. ThreadLocal is not an implementation of thread localization, but a thread-local variable.
That is to say, every thread that uses this variable must provide a copy of this variable, and each thread changing the value of this variable only changes the value of this copy, without affecting the value of this variable in other threads. ThreadLocal is a data sharing that isolates multiple threads, and there is no shared resource between multiple threads, so there is no need to synchronize threads.
Take a look at the code:
public class HibernateUtil { public static final SessionFactory sessionFactory; public static final ThreadLocal session = new ThreadLocal(); static{ try{ Configuration configuration=new Configuration().configure(); sessionFactory = configuration.buildSessionFactory(); }catch (Throwable ex){ System.err.println("Initial SessionFactory creation failed. " + ex); throw new ExceptionInInitializerError(ex); } } public static Session currentSession() throws HibernateException{ Session s = (Session) session.get(); if (s == null) { s = sessionFactory.openSession(); session.set(s); } return s; } public static void closeSession() throws HibernateException { Session s = (Session) session.get(); if (s != null) s.close(); session.set(null); } } Thank you for reading, the above is the content of "Hibernate Session what to use", after learning this article, I believe that everyone has a deeper understanding of Hibernate Session what to use this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.