new Configuration()。configure()。buildSessionFactory()在每次调用时创建新连接

时间:2017-09-18 06:41:59

标签: java hibernate

我是hibernate中的新用户,我的代码工作正常,但我在Controller class的每次调用中都检查过,在mysql中创建了一个新的连接线程,该线程处于睡眠状态。我的代码是 -

hibernate.cfg.xml档案:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hib_db_netbean</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">system</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MariaDBDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.c3p0.min_size">2</property>
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.timeout">300</property>
        <property name="hibernate.c3p0.max_statements">50</property>
        <property name="hibernate.c3p0.idle_test_period">3000</property>

        <mapping class="com.hib.collection.UserDetails"/>

    </session-factory>
</hibernate-configuration>

我的实体类(UserDetails.java):

package com.hib.collection;

import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;

@Entity
@Table(name = "USER_TABLE")
public class UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int user_id;

    @Column(name = "user_name")
    private String username;

    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

}

控制器类 - CallHibernate.java

package com.hib.collection;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

@WebServlet(name = "CallHibernate", urlPatterns = {"/CallHibernate"})
public class CallHibernate extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        UserDetails user = new UserDetails();

        SessionFactory sessionFactory;
        Session session = null;

        user.setUsername("User Name");

        try (PrintWriter out = response.getWriter()) {

            sessionFactory = new   Configuration().configure().buildSessionFactory();
            session = sessionFactory.openSession();
            session.beginTransaction();

            // Insert The Data In the Table
            session.save(user);
            session.getTransaction().commit();

            session.close();


        }
         catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        } finally {
            session.close();
        }
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    }

MySQL Workbench连接状态:

enter image description here

1 个答案:

答案 0 :(得分:-2)

使用单例设计模式。

通过以下链接关注单身设计模式:

http://www.onlinetutorialspoint.com/hibernate/singleton-hibernate-sessionfactory-example.html