使用 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();
}
%>