0

使用 Hibernate 构建一个非常简单的数据库,但请注意,我经常收到 Glassdoor 服务器错误,告诉我“连接太多”。

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

这是我在 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();
        }
 %>
4

1 回答 1

1

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

通常,您会将休眠工厂初始化放在上下文初始化之上,然后您应该在每个请求开始时从工厂获取一次会话。

@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 快速入门指南

于 2012-12-01T00:57:07.710 回答