简体   繁体   中英

c3p0 with hibernate don't close connection

I have a project using Hibernate JPA with persistence.xml and EntityManager

<property name="connection.provider_class"  value="org.hibernate.connection.C3P0ConnectionProvider"/>
     <!-- Important -->


  <property name="hibernate.c3p0.min_size" value="10"/>
  <property name="hibernate.c3p0.max_size" value="20"/>
    <property name="hibernate.c3p0.acquire_increment" value="1"/>
  <property name="hibernate.c3p0.minPoolSize" value="1"/>
  <property name="hibernate.c3p0.timeout" value="1800"/>
  <property name="hibernate.c3p0.max_statements" value="50"/>
  <property name="hibernate.c3p0.idle_test_period" value="3000"/>

I have a class EntityManagerHelper that is handling the EntityManager and EntityManagerFactory

public class EntityManagerHelper {

static Logger logger = Logger.getLogger(EntityManagerHelper.class);

private static final EntityManagerFactory emf; 
private static final ThreadLocal<EntityManager> threadLocal;

static {
    logger.warn("Criando EM Factory");
    emf = Persistence.createEntityManagerFactory("persistencia");      
    threadLocal = new ThreadLocal<EntityManager>();
}

public static EntityManager getEntityManager() {
    EntityManager em = threadLocal.get();

    if (em == null) {
        em = emf.createEntityManager();
        threadLocal.set(em);
    }
    return em;
}

public static void closeEntityManager() {
    EntityManager em = threadLocal.get();
    if (em != null) {
        logger.info("fechando Transação");
        em.close();
        threadLocal.set(null);
    }
}

public static void closeEntityManagerFactory() {
    logger.warn("Destroindo EM Factory");
    emf.close();
}

public static void beginTransaction() {
    logger.info("iniciando Transação");
    getEntityManager().getTransaction().begin();
}

public static void rollback() {
    getEntityManager().getTransaction().rollback();
}

public static void commit() {
    logger.info("commitando Transação");
    getEntityManager().getTransaction().commit();
} 
}

and I use this filter

public class JPAFilter implements Filter {

@Override
public void destroy() {}

@Override
public void init(FilterConfig fc) throws ServletException {}

@Override
public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {

        try {
            req.setAttribute("EntityManager", EntityManagerHelper.getEntityManager());
            EntityManagerHelper.beginTransaction();
            chain.doFilter(req, res);
            EntityManagerHelper.commit();
        } catch (RuntimeException e) {

            if ( EntityManagerHelper.getEntityManager() != null && EntityManagerHelper.getEntityManager().isOpen()) 
                EntityManagerHelper.rollback();
            throw e;

        } finally {
            EntityManagerHelper.closeEntityManager();
        }
}
public EntityManager  getEntityManager(){
    return EntityManagerHelper.getEntityManager();
}

}

but the connections are not closed with the seat, see below

PostgreSQL

Please point me to where I'm wrong.

Thanks in advance.

The connection pool is not about closing connection. Its all about the contrary situation - to keep connections alive and reuse them. It is much more efficient to keep live idle connections than to reestablish it on demand.

As a bonus, check https://www.mchange.com/projects/c3p0/ and Appendix C as some of your configuration properties are surplus and/or does not have any effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM