MySQL / Hibernate连接错误太多

时间:2012-12-01 00:05:44

标签: sql hibernate

使用Hibernate组建一个非常简单的数据库,但请注意,我经常得到Glassdoor服务器错误,告诉我“Too Many Connections”。

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

以下是我在updateEntry.jsp中所做的一个例子 - 任何blantant问题?如果没有,我可以发布我的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();
        }
 %>

1 个答案:

答案 0 :(得分:0)

您不应该在JSP中初始化hibernate,只需在上下文启动时初始化一次。

通常你会在上下文初始化时把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中获取实体管理器。

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

我认为你应该仔细阅读Hibernate Quickstart Guide