我正在使用C3P0连接池,hibernate和CDI到EntityManagerProducer 但是在MySql ShowProcessList
中My 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();
}
}
我如何移除连接并释放给其他用户?
答案 0 :(得分:0)
连接池背后的整个想法是保持连接打开并在处理后续请求时重用它们。因为建立与数据库的新连接是一项代价高昂的操作。