the first picture:
the second picture:
首先来两张图
第一张图:大概意思是one.jsp把数据提交到servlet,(这里已开启一个新的事务,也是一个新的session),然后调用servive层,再调用dao层,最后在数据库查询数据,然后把数据返回到dao层,service层,servlet,最后在two.jsp中显示查询结果。在servlet向two.jsp提交查询结果之前,先解析jsp,然后提交事务,这时session也就关闭了。
总结:一次请求开启一个新的session
第二张图:jsp页面向servlet,service,dao层提交请求,要先经过过滤器,最后到数据库;返回数据也是一样,最后也要经过过滤器才能在jsp页面中显示查询结果
举个例子:
HibernateUtil .java
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
SessionFactory sf= cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build());
return sf;
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getSession(){
Session session = threadLocal.get();
if(session == null || !session.isOpen()){ //判断session是否为空,或者没打开
session = getSessionFactory().getCurrentSession();
threadLocal.set(session);
}
return session;
}
}
GenericDaoImpl .java//相当于baseDaoImpl,接口的 父类
public class GenericDaoImpl implements GenericDao {
private Class genericEntity;
public Class getGenericEntity() {
return genericEntity;
}
public void setGenericEntity(Class genericEntity) {
this.genericEntity = genericEntity;
}
@Override
public void addEntity(Object obj) {
// TODO Auto-generated method stub
try{
Session session = HibernateUtil.getSession();
//session.beginTransaction();
session.save(obj);
//session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void deleteEntity(Object obj) {
// TODO Auto-generated method stub
try{
Session session = HibernateUtil.getSession();
//session.beginTransaction();
session.delete(obj);
//session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void updateEntity(Object obj) {
// TODO Auto-generated method stub
try{
Session session = HibernateUtil.getSession();
//session.beginTransaction();
session.update(obj);
//session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public Object findById(Serializable entityId) {
// TODO Auto-generated method stub
Object obj = null;
try{
Session session = HibernateUtil.getSession();
//session.beginTransaction();
obj = session.load(genericEntity, entityId);
//session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}
return obj;
}
@Override
public List findAll() {
// TODO Auto-generated method stub
List list = new ArrayList();
try{
Session session = HibernateUtil.getSession();
//session.beginTransaction();
list = session.createQuery("from "+genericEntity.getName()).list();
//session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}
return list;
}
}
过滤器OpenSessionInViewFilter .java
public class OpenSessionInViewFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("客户端请求服务器执行代码");
Session session = HibernateUtil.getSession();
session.beginTransaction();
chain.doFilter(request,response);
System.out.println("服务器响应客户器执行代码");
session.getTransaction().commit();
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
运行程序后台显示信息为:
可见请求页面先经过过滤器,最后返回结果集时也要经过过滤器。