Opensessioninview mode solves the problem:
* Hibernate things boundary problem
* Hibernate delayed loading exception due to session shutdown
Thing boundary:
The completion of a thing should be done in the business layer, but the creation of things is done in the data layer, which will inevitably result in the business layer and the data layer of the coupling enhancement.
Deferred load Exceptions:
As you know, lazy loading uses a dynamic proxy mechanism that accesses the database only when the object is actually used, but if a commit is made at the data layer, the session is closed, and then the access to the data results in a lazy loading exception, so we have to increase the session life cycle, Closes the session after accessing the data.
So we can solve the above problem through the Opensessioninview mode.
The Opensessioninview pattern requires a very important class: ThreadLocal
Opensessioninview mode:
* You need to build a filter to put the creation and submission of things in a filter.
* Increase the effective range of the session, put the session in the current line thread (ThreadLocal), so that the session is valid within the current thread, and the current line range access is the same session.
Opensessioninview Mode Disadvantages:
* Concurrency weakened
* Increased memory consumption
1. Entity classes:
Java code
- Package Com.yx.zzg.pojo;
- Import Java.util.Date;
- Public class User {
- private String ID;
- private String username;
- private String password;
- private Date Createtime;
- private Date EndTime;
- Public String getId () {
- return ID;
- }
- public void SetId (String id) {
- this.id = ID;
- }
- Public String GetUserName () {
- return username;
- }
- public void Setusername (String username) {
- this.username = Username;
- }
- Public String GetPassword () {
- return password;
- }
- public void SetPassword (String password) {
- This.password = password;
- }
- Public Date Getcreatetime () {
- return createtime;
- }
- public void Setcreatetime (Date createtime) {
- this.createtime = createtime;
- }
- Public Date Getendtime () {
- return endTime;
- }
- public void Setendtime (Date endTime) {
- this.endtime = endTime;
- }
- }
2. Mapping File:
Java code
- <?xml version="1.0"?>
- <! DOCTYPE hibernate-mapping Public
- "-//hibernate/hibernate Mapping DTD 3.0//en"
- "HTTP://HIBERNATE.SOURCEFORGE.NET/HIBERNATE-MAPPING-3.0.DTD" >
- package="Com.yx.zzg.pojo" >
- <class Name="User" table="T_user" >
- <id name="id" >
- <!--Specify a primary key generation strategy--
- <generator class="uuid"/>
- </id>
- <property name="username" unique-key="true"/>
- <property name="password"/>
- <property name="Createtime"/>
- <property name="EndTime"/>
- </class>
3. Provide a filter:
Java code
- Package com.yx.zzg.servelt;
- Import java.io.IOException;
- Import Javax.servlet.Filter;
- Import Javax.servlet.FilterChain;
- Import Javax.servlet.FilterConfig;
- Import javax.servlet.ServletException;
- Import Javax.servlet.ServletRequest;
- Import Javax.servlet.ServletResponse;
- Import org.hibernate.Session;
- Import org.hibernate.Transaction;
- Import Com.yx.zzg.util.HibernateUtil;
- Public class Opensessioninview implements Filter {
- public Void Destroy () {
- }
- public void DoFilter (ServletRequest request, servletresponse response,
- Filterchain filter) throws IOException, servletexception {
- Transaction tx = null;
- Session session = null;
- try {
- Session = Hibernateutil.getthreadlocalsession ();
- tx = Session.begintransaction ();
- Filter.dofilter (request, response);
- Tx.commit ();
- } catch (Exception e) {
- if (tx! = null) {
- Tx.rollback ();
- throw New RuntimeException (E.getmessage (), E);
- }
- } finally {
- Hibernateutil.colsesession ();
- }
- }
- public void init (Filterconfig arg0) throws servletexception {
- }
- }
Configuring Filters in 4.web.xml
Java code
- <filter>
- <filter-name>OpenSessionInView</filter-name>
- <filter-class>com.yx.zzg.servelt.opensessioninview</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>OpenSessionInView</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
5.HibernateUtil class:
Java code
- Package com.yx.zzg.util;
- Import org.hibernate.Session;
- Import Org.hibernate.SessionFactory;
- Import org.hibernate.cfg.Configuration;
- Public class Hibernateutil {
- private static sessionfactory sessionfactory;
- //Create a Threadlocal object
- private static ThreadLocal threadsession = new ThreadLocal ();
- Private Hibernateutil () {
- }
- Static {
- Configuration cfg = new configuration (). Configure ();
- Sessionfactory = Cfg.buildsessionfactory ();
- }
- /**
- * Gets the session from the current thread, if not, gets the session and puts the session into the current thread
- * @return
- */
- public static Session getthreadlocalsession () {
- Session Session = (session) Threadsession.get ();
- if (session = = null) {
- Session = GetSession ();
- Threadsession.set (session);
- }
- return session;
- }
- /**
- * Close the session and set the session in the current thread to NULL
- */
- public static void Colsesession () {
- Session Session = (session) Threadsession.get ();
- if (session! = null) {
- Session.close ();
- Threadsession.set (null);
- }
- }
- public static Session getsession () {
- return sessionfactory.opensession ();
- }
- }
6. Data layer Method:
Java code
- Public User Finduserbyid (String ID) {
- User user = (user) hibernateutil.getthreadlocalsession (). Load (
- User. class, ID);
- return user;
- }
Go Opensessioninview mode