【问题标题】:Hibernate not Closing MySql ConnectionHibernate 不关闭 MySql 连接
【发布时间】:2016-09-04 00:14:36
【问题描述】:

我正在使用 C3P0 连接池、休眠和 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 个连接。 为什么休眠不关闭连接?使用 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();
    }
}

我如何删除连接并释放给另一个用户?

【问题讨论】:

  • 对不起,看到这样的处理方法我很奇怪。为什么不关闭作为参数传递的实体管理器?请在您的 public void close(@Disposes EntityManager manager) 方法中执行 manager.close()

标签: mysql jpa cdi


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-02-17
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 2015-02-20
    • 1970-01-01
    相关资源
    最近更新 更多