Java/Hibernate - 异常:内部连接池已达到其最大大小,当前没有可用连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38960921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:56:59  来源:igfitidea点击:

Java/Hibernate - Exception: The internal connection pool has reached its maximum size and no connection is currently available

javamysqlsqlhibernate

提问by Alessandro

I'm using Hibernate for the first time for a University project and I'm a bit of a newbie. I think I followed all the instructions given by my professor and by some tutorials I read, but I keep getting the Exception that is in the title:

我第一次在大学项目中使用 Hibernate,我是个新手。我想我遵循了教授给出的所有说明以及我阅读的一些教程,但我一直收到标题中的异常:

Exception in thread "main" org.hibernate.HibernateException: The internal connection pool has reached its maximum size and no connection is currently available!

What I'm trying to do is simply storing an object (AbitazioneDB) into a MySql Database that I have already created. This is my configuration file:

我想要做的只是将一个对象(AbitazioneDB)存储到我已经创建的 MySql 数据库中。这是我的配置文件:

<?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>
        <!-- Connection to the database -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/AllarmiDomesticiDB</property>

        <!-- Credentials -->
        <property name="hibernate.connection.username">root</property>
        <property name="connection.password">password</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <property name="show_sql">true</property>

        <!-- Entity -->

        <mapping class="it.allarmiDomestici.centraleOperativaRemota.dbWrapper.AbitazioneDB" />

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

This is my AbitazioneDBclass (I will omitt getters and setters):

这是我的AbitazioneDB类(我将省略 getter 和 setter):

@Entity
@Table(name="abitazioni")
public class AbitazioneDB {

    @Id
    @GeneratedValue
    @Column(name="id")
    private Integer id;

    @Column(name="indirizzo")
    private String indirizzo;

    @Column(name="nome_proprietario")
    private String nomeProprietario;

    @Column(name="tel_proprietario")
    private String telProprietario;

    public AbitazioneDB(){
        super();
    }

    public AbitazioneDB save(){

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session session = sf.getCurrentSession();
        session.beginTransaction();

        Integer id = (Integer) session.save(this);
        this.setId(id);

        session.getTransaction().commit();      
        session.close();

        return this;
    }
}

This is my HibernateUtilclass:

这是我的HibernateUtil类:

public class HibernateUtil {

    private static SessionFactory sessionFactory;

    private static SessionFactory createSessionFactory() {
        sessionFactory = new Configuration().configure().buildSessionFactory();
        return sessionFactory;
    }

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null)
            sessionFactory = createSessionFactory();

        return sessionFactory;
    }
}

And this is my TestHibernateclass, with the mainmethod:

这是我的TestHibernate类,main方法如下:

public class TestHibernate {

    public static void main(String[] args) {
        AbitazioneDB ab = new AbitazioneDB();

        ab.setIndirizzo("Via Napoli, 29");
        ab.setNomeProprietario("Mario Rossi");
        ab.setTelProprietario("3333333333");

        ab.save();

        System.out.println("Ok!");

    }
}

When I run TestHibernateI always get that exception and I have no idea why. Maybe I should change the connection.pool_sizeproperty, but if I do so it seems that I only get more errors. Can someone help me?

当我运行TestHibernate 时,我总是得到那个异常,我不知道为什么。也许我应该更改connection.pool_size属性,但如果我这样做,似乎只会得到更多错误。有人能帮我吗?

采纳答案by Pritam Banerjee

Try this :

试试这个 :

HibernateUtil:

休眠实用程序:

public static Session getHibernateSession() {

    final SessionFactory sf = new Configuration()
        .configure("yourConfig.cfg.xml").buildSessionFactory();

    // factory = new Configuration().configure().buildSessionFactory();
    final Session session = sf.openSession();
    return session;
    }

And instead of using SessionFactory use Session:

而不是使用 SessionFactory 使用 Session:

final Session session = HibernateUtil.getHibernateSession();

And then use:

然后使用:

 Transaction tx = session.beginTransaction();
 // all your methods
 tx.commit();
 session.close();

Hope this helps.

希望这可以帮助。

And you can actually not have that connection pool property in your hbm if you don't need it.

如果您不需要它,您实际上可以在 hbm 中没有该连接池属性。

回答by 11thdimension

Your connection pool has exhausted the connections because you're using single connection in the pool.

您的连接池已耗尽连接,因为您在池中使用单个连接。

<property name="connection.pool_size">1</property>

Change this property to something that you'll be comfortable with, like 100

将此属性更改为您会感到满意的内容,例如 100

回答by user5575385

<property name="connection.pool_size">1</property>

Try to change the connection pool size to a bigger size, like 100 or so.

尝试将连接池大小更改为更大的大小,例如 100 左右。

<property name="connection.pool_size">100</property>