这是我的hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.release_mode">after_statement</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.c3p0.maxPoolSize">5</property>
<property name="hibernate.c3p0.maxIdleTime">5</property>
<property name="hibernate.c3p0.maxStatements">1</property>
<property name="hibernate.c3p0.minPoolSize">1</property>
<mapping resource="hbm/Candidati.hbm.xml"/>
<mapping resource="hbm/Comune.hbm.xml"/>
<mapping resource="hbm/ElezioniComuni.hbm.xml"/>
<mapping resource="hbm/Elezioni.hbm.xml"/>
<mapping resource="hbm/Liste.hbm.xml"/>
<mapping resource="hbm/Seggio.hbm.xml"/>
<mapping resource="hbm/Sindaco.hbm.xml"/>
<mapping resource="hbm/Utenti.hbm.xml"/>
<mapping resource="hbm/UtentiElezioni.hbm.xml"/>
<mapping resource="hbm/UtentiSeggio.hbm.xml"/>
<mapping resource="hbm/VotiCandidati.hbm.xml"/>
<mapping resource="hbm/VotiSindaco.hbm.xml"/>
<mapping resource="hbm/Province.hbm.xml"/>
<mapping resource="hbm/Regioni.hbm.xml"/>
<mapping resource="hbm/VotiListe.hbm.xml"/>
<mapping resource="hbm/VotiSpeciali.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这是我的连接类
package Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class Connessione {
HttpServletRequest request=ServletActionContext.getRequest();
HttpSession session=request.getSession();
SessionFactory factory=(SessionFactory)session.getAttribute("hibernate");
public void connect(){
if(factory==null){
Configuration configuration=new Configuration().configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
factory=configuration.buildSessionFactory(serviceRegistry);
session.setAttribute("hibernate", factory);
}
}
public SessionFactory getFactory() {
return factory;
}
public void setFactory(SessionFactory factory) {
this.factory = factory;
}
}
我的crud类的方法
public ArrayList<Elezioni> SelectEle(){
conn.connect();
SessionFactory factory = (SessionFactory) session.getAttribute("hibernate");
Session session = factory.openSession();
listaElezioni=new ArrayList<Elezioni>();
listaElezioni =(ArrayList<Elezioni>) session.createQuery("from Elezioni" ).list();
System.out.println("Taglia:"+listaElezioni.size());
session.close();
return listaElezioni;
}
Ele class在客户端获取延迟加载。
hibernate为1600行数据库表创建超过20个连接
这是问题所在。此代码
<property name="hibernate.c3p0.maxPoolSize">5</property>
<property name="hibernate.c3p0.maxIdleTime">5</property>
<property name="hibernate.c3p0.maxStatements">1</property>
<property name="hibernate.c3p0.minPoolSize">1</property>
减少连接数但锁定应用程序。请求休息待处理状态
添加信息。这是Struts2应用程序 我怎么能解决这个问题?
答案 0 :(得分:1)
你做了一件完全不正确的事。每个HTTP会话都有一个新的会话工厂。对于完全应用,您应该有一个SessionFactory
。
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
SessionFactory factory =(SessionFactory)session.getAttribute("hibernate");
if(factory == null){
}
每个新用户都有factory == null
。因此,每个用户都拥有自己的SessionFactory
连接。
您可以使用SessionFactroy
创建和销毁ServletContextListener
。