mysql - Hibernate 不关闭 MySql 连接

标签 mysql jpa cdi

我正在使用 C3P0 连接池、hibernate 和 CDI 到 EntityManagerProducer 但在MySql ShowProcessList

enter image description here

我的 Persistence.xml

<!-- Nao remover AutoReconect=true -->
            <property name="javax.persistence.jdbc.url"
                value="jdbc:mysql://localhost:3307/db_simulados?autoReconnect=true" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="1994" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />

            <!-- validate | update | create | create-drop -->
            <property name="connection.useUnicode" value="true" />
            <property name="connection.characterEncoding" value="uf-8" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />

            <!-- C3P0 -->
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="connection.provider_class"
                value="org.hibernate.connection.C3P0ConnectionProvider" />

            <property name="hibernate.c3p0.min_size" value="1" />
            <property name="hibernate.c3p0.max_size" value="5" />
            <property name="hibernate.c3p0.acquire_increment" value="1" />
            <property name="hibernate.c3p0.idle_test_period" value="300" />
            <property name="hibernate.c3p0.max_statements" value="0" />
            <property name="hibernate.c3p0.timeout" value="100" />

在我的 C3P0 中,我将其配置为最多 5 个连接。 为什么 Hibernate 不关闭连接?使用 CDI 时,entityManager 通常使用 @disposes 关闭,但在 ShowProcessList 中连接未关闭。

我的EntityManagerProducer

public class JpaUtil {

    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("simuladosPU");

    @Produces
    @RequestScoped
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void close(@Disposes EntityManager manager) {
        // Called Normally.
        System.out.println("Fechou!");
        this.getEntityManager().close();
    }
}

如何删除连接并释放给其他用户?

最佳答案

别担心。使用任何数据库连接池(如 C3P0)时,这是理想的行为。

连接池背后的整体思想是保持连接打开并在处理后续请求时重用它们。因为建立与数据库的新连接是一项代价高昂的操作。

关于mysql - Hibernate 不关闭 MySql 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39312442/

相关文章:

mysql - 有没有更好的方法来进行数据迁移?

mysql - 多对多关系和复杂的 where 语句

java - 是什么导致了这个 "No Persistence provider for EntityManager named ___"异常?

java - Mockito 不识别类

servlets - Java EE 6 企业应用程序 : Warning "Unsatisfied dependency: no bean matches the injection point" when injecting a bean

mysql - 如何将包含反斜杠的csv文件导入MySQL

mysql - 来自 Laravel 5.5 中 HasManyThrough 的 ManyToMany 关系的 GroupBy 结果

java - spring jpa 审计 lastmodifiedby 和 lastmodifiedDate 是可以的,但是 createdBy 和 createdDate 注解总是 null

hibernate - persistence.xml 中 jpa 的 C3p0 连接池不起作用?

jsf - 如何在Tomcat上安装和使用CDI?