简体   繁体   中英

hibernate too many connection

this is my 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>

this is my connection class

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;
    }

}

method of my crud class

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 fetch lazy loading on Customer.

hibernate create over than 20 connection for 1600 row of database table

this is problem.

this code

 <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>

reduce number of connection but lock application. request rest in pending status

add information. this is Struts2 application how can i solve this?

You do an entirely incorrect thing. You have a new session factory for an every HTTP session. You should have a one SessionFactory for an entirely application.

HttpServletRequest request = ServletActionContext.getRequest();  
HttpSession session = request.getSession();
SessionFactory factory =(SessionFactory)session.getAttribute("hibernate");

if(factory == null){

}

You have factory == null for every new user. So every user has own SessionFactory with connections.

You can create and destroy the SessionFactroy using ServletContextListener .

storing Hibernate SessionFactory with Struts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM