【发布时间】:2016-09-04 00:14:36
【问题描述】:
我正在使用 C3P0 连接池、休眠和 CDI 到 EntityManagerProducer 但在 MySql ShowProcessList 中
我的 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()。