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

2022-09-03 17:28:18

我第一次在大学项目中使用Hibernate,我有点新手。我想我遵循了我的教授和我读过的一些教程给出的所有说明,但我一直得到标题中的例外:

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

我试图做的是简单地将一个对象(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>

这是我的 AbitazioneDB 类(我将省略 getters 和 setters):

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

这是我的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;
    }
}

这是我的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!");

    }
}

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


答案 1

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

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

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


答案 2

试试这个 :

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

而不是使用SessionFactory使用Session:

final Session session = HibernateUtil.getHibernateSession();

然后使用:

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

希望这有帮助。

如果你不需要它,你实际上不能在你的hbm中有那个连接池属性。


推荐