2012-12-01 160 views
0

使用Hibernate将一个非常简单的数据库放在一起,但注意到很多时候我得到Glassdoor服务器错误告诉我“太多连接”。MySQL /休眠太多连接错误

我认为这是因为我更新/添加/删除数据库项目时没有正确关闭连接。

下面是我在updateEntry.jsp中做什么的一个例子 - 任何明显的问题?如果没有,我可以张贴我的removeEntry.jsp和newEntry.jsp:

<%@page import="java.util.Date" %> 
<%@page import="org.hibernate.Session" %> 
<%@page import="org.hibernate.SessionFactory" %> 
<%@page import="org.hibernate.Transaction" %> 
<%@page import="org.hibernate.cfg.Configuration" %> 
<%@page import="java.util.Date" %> 
<% 
    String nId = request.getParameter("pID"); 
    String naddress = request.getParameter("pAddress"); 
    String nstatus = request.getParameter("pStatus"); 
    String nassigned = request.getParameter("pAssigned"); 
    String nnote = request.getParameter("pNote"); 

     if (!naddress.equals("undefined")) 
     { 
      // This step will read hibernate.cfg.xml and prepare hibernate for use 
      org.hibernate.SessionFactory sessionFactory1 = new org.hibernate.cfg.Configuration().configure().buildSessionFactory(); 
      org.hibernate.Session session1 = sessionFactory1.openSession(); 

      org.hibernate.Query query1 = session1.createQuery("update Leads set Address = :naddr where Id = :nid"); 
      query1.setParameter("nid", nId); 
      query1.setParameter("naddr", naddress); 
      query1.executeUpdate(); 
      //out.println("Update successfully with: " + naddress); 
      // Actual contact insertion will happen at this step 
      session1.flush(); 
      session1.close(); 
     } 
     if (!nstatus.equals("undefined")) 
     { 
      // This step will read hibernate.cfg.xml and prepare hibernate for use 
      org.hibernate.SessionFactory sessionFactory2 = new org.hibernate.cfg.Configuration().configure().buildSessionFactory(); 
      org.hibernate.Session session2 = sessionFactory2.openSession(); 

       org.hibernate.Query query2 = session2.createQuery("update Leads set Status = :nstatus where Id = :nid"); 
       query2.setParameter("nid", nId); 
       query2.setParameter("nstatus", nstatus); 
       query2.executeUpdate(); 
       //out.println("Update successfully with: " + nstatus); 
       // Actual contact insertion will happen at this step 
       session2.flush(); 
       session2.close(); 
     } 
     if (!nassigned.equals("undefined")) 
     { 
      // This step will read hibernate.cfg.xml and prepare hibernate for use 
      org.hibernate.SessionFactory sessionFactory3 = new org.hibernate.cfg.Configuration().configure().buildSessionFactory(); 
      org.hibernate.Session session3 = sessionFactory3.openSession(); 
      org.hibernate.Query query3 = session3.createQuery("update Leads set Assigned = :nassigned where Id = :nid"); 
      query3.setParameter("nid", nId); 
      query3.setParameter("nassigned", nassigned); 
      query3.executeUpdate(); 
      //out.println("Update successfully with: " + nassigned); 
      // Actual contact insertion will happen at this step 
      session3.flush(); 
      session3.close(); 
     } 
     if (!nnote.equals("undefined")) 
     { 
      // This step will read hibernate.cfg.xml and prepare hibernate for use 
      org.hibernate.SessionFactory sessionFactory4 = new org.hibernate.cfg.Configuration().configure().buildSessionFactory(); 
      org.hibernate.Session session4 = sessionFactory4.openSession(); 
      org.hibernate.Query query4 = session4.createQuery("update Leads set Notes = :nnote where Id = :nid"); 
      query4.setParameter("nid", nId); 
      query4.setParameter("nnote", nnote); 
      query4.executeUpdate(); 
      //out.println("Update successfully with: " + nnote); 
      // Actual contact insertion will happen at this step 
      session4.flush(); 
      session4.close(); 
     } 
%> 

回答

0

在JSP中你不应该初始化休眠,你只当上下文开始初始化一次。

通常情况下,您会在上下文初始化时放置hibernate工厂初始化,然后您应该在每次请求开始时从工厂获取一次会话。

@WebListener 
public class HibernateListener implements ServletContextListener { 
    public static final String ENTITY_MANAGER = "entity.manager"; 

    public void contextInitialized(ServletContextEvent evt) { 
     SessionFactory sessionFactory = new Configuration() 
       .configure().buildSessionFactory(); 
     evt.getServletContext() 
       .setAttribute(ENTITY_MANAGER, sessionFactory); 
    } 

    public void contextDestroyed(ServletContextEvent evt) { 
     SessionFactory sessionFactory = (SessionFactory) evt.getServletContext() 
       .getAttribute(ENTITY_MANAGER); 
     sessionFactory.close(); 
    } 

} 

然后,您可以从servletContext中获取会话选择entityManager。

<% SessionFactory factory =(SessionFactory) session.getServletContext() 
     .getAttribute(HibernateListener.ENTITY_MANAGER); %> 

我想你应该仔细阅读Hibernate Quickstart Guide