Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the Hibernate session problem

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to solve the Hibernate session problem". In the daily operation, I believe that many people have doubts about how to solve the Hibernate session problem. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer the doubt of "how to solve the Hibernate session problem"! Next, please follow the editor to study!

No matter immediate loading or delayed loading, you must connect to the database. In java, connecting to the database depends on java.sql.Connection. In Hibernate session, it is a high-level encapsulation of Connection. A session corresponds to a Connection. To achieve delayed loading, there must be session. And to delay loading, it must be the same session. It is not possible to use another session to delay loading the proxy object of the previous session. We all know that Connection must be closed after use, so how can we ensure that a session, that is, a Connection, is always used in a http request? Also make sure that the http request is closed correctly after the end.

Okay, now we know the problem we're trying to solve.

1. How to ensure that session is shut down correctly after the end of http request

two。 How to ensure that the same session is always used during a http request

* the question is easy to think of, using a filter

Public void doFilter (ServletRequest request

ServletResponse response,FilterChain filterChain) {

Try {

FilterChain.doFilter (request, response)

}

Catch (IOException e) {

E.printStackTrace ()

}

Catch (ServletException e) {

E.printStackTrace ()

}

Finally {

Try {

HibernateUtil.commitTransaction ()

}

Catch (Exception e) {

HibernateUtil.rollbackTransaction ()

}

Finally {

HibernateUtil.closeSession ()

}

}

}

To solve the second problem, we must first figure out how http requests are implemented in java. In java, a request is a thread, such as the popular web container Tomcat, which often uses thread pool mechanism, that is to say, there are n threads in the pool. Whenever there is a http request, a thread object is randomly taken from the thread pool to process the request. In fact, multiple requests may or may not be using the same thread, which is random. To ensure that the same session is used in the whole request, the easiest thing to think of is to bind the session to the thread. Using ThreadLocal in java can easily bind variables. Each thread has its own ThreadLocal, and this ThreadLocal will be destroyed along with the destruction of the thread. Since each thread has so many threads, naturally there will be no effect, so binding the session in the thread is a * choice. * I send out the code related to Hibernate session.

Here is an example of a call: view plaincopy to clipboardprint?

Public static void main (String [] args) throws Exception {

HibernateUtil.initSessionFactory (new File (Test.class.getClassLoader)

(.getResource ("hibernate.cfg.xml") .getFile ())

Session session = HibernateUtil.getSession ()

HibernateUtil.transaction ()

User u = new User ()

U.setName ("test")

Session.save (u)

HibernateUtil.commitTransaction ()

HibernateUtil.closeSession ()

}

At this point, the study of "how to solve the Hibernate session problem" 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report