码迷,mamicode.com
首页 > 数据库 > 详细

hibernate基础14:OpenSessionInView(抽取web访问时对数据库开关事务)

时间:2020-05-24 19:25:20      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:cep   row   EDA   关闭   over   对象   hibernate   stat   private   

1、使用过滤器filter

package com.project.filter;

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.project.util.HibernateUtil;

public class OpenSessionInViewFilter implements Filter{

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {
        Session session = null;
        Transaction ts = null;
        try {
            session = HibernateUtil.getSession();
            ts = session.beginTransaction();
            System.out.println("---------begin Filter---------");
            chain.doFilter(req, resp);
            System.out.println("---------end Filter---------");
            ts.commit();
        } catch (Exception e) {
            if(ts!=null)ts.rollback();
            throw e;
        }finally{
            HibernateUtil.closeSession();
        }
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }

}

2、hibernateUtil类

package com.project.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static Configuration cfg = null;
    private static StandardServiceRegistry registry = null;
    private static SessionFactory factory = null;
    private static ThreadLocal<Session> sessionLocal = null;
    
    static{
        //1、读取hibernate.cfg.xml配置文件
        cfg = new Configuration().configure();
        //2、创建服务注册器
        registry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
        //3、新建sessionFactory对象
        //sessionFactory是一个重量级对象,在一个应用只需一个即可,是进程级别的对象,可以在集群中使用,操作完后不释放
        factory = cfg.buildSessionFactory(registry);
        //4、创建threadLocal(成员私有,线程安全)
        sessionLocal = new ThreadLocal<>();
    }
    //获取session对象
    public static Session getSession(){
        //session不为空且session未被打开时,session设置为null
        if(sessionLocal.get() != null && !sessionLocal.get().isOpen()){
            sessionLocal.set(null);
        }
        //session为null时,创建session
        if(sessionLocal.get() == null){
            sessionLocal.set(factory.openSession());
        }
        return sessionLocal.get();
    }
    
    //关闭session
    public static void closeSession(){
        if(sessionLocal.get() != null){
            //session调用close不一定清除session对象
            sessionLocal.get().close();
            //所以sessionLocal中session对象设置为null
            sessionLocal.set(null);
        }
    }
}

3、dao使用

package com.project.dao.impl;

import java.util.List;

import org.hibernate.Session;

import com.project.dao.EmployeeDao;
import com.project.pojo.Employee;
import com.project.util.HibernateUtil;

public class EmployeeDaoImpl implements EmployeeDao{

    @Override
    public List<Employee> find() {
        /**
         * HibernateUtil.getSession()与filer中使用的是同一个session
         */
        Session session = HibernateUtil.getSession();
        List<Employee> list = session.createCriteria(Employee.class).list();
        return list;
    }

}

 

hibernate基础14:OpenSessionInView(抽取web访问时对数据库开关事务)

标签:cep   row   EDA   关闭   over   对象   hibernate   stat   private   

原文地址:https://www.cnblogs.com/chai-blogs/p/12952211.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!