使用hikaricp连接池获得与spring-boot和olingo的泄漏连接

问题描述

因此,我找到了有关使用spring-boot和olingo创建odata服务的教程

https://www.baeldung.com/olingo

但是问题是我遇到了连接泄漏。在服务器开发方面,我并不是真正的专家,因此我很难找到泄漏发生的地方。我认为这里的2个重要源代码是我的DoeOdataJpaServiceFactory.java

package gov.doe.app;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityGraph;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaDelete;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaUpdate;
import javax.persistence.metamodel.metamodel;
import javax.servlet.http.HttpServletRequest;

import org.apache.olingo.odata2.api.processor.ODataContext;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAServiceFactory;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import gov.doe.app.JerseyConfig;

/**
 * ODataJPAServiceFactory implementation for our sample domain 
 * @author Philippe
 *
 */
@Component
public class DoeODataJPAServiceFactory extends ODataJPAServiceFactory {

    private static final Logger log = LoggerFactory.getLogger(DoeODataJPAServiceFactory.class);

    public DoeODataJPAServiceFactory() {
        // Enable detailed error messages (useful for debugging)
        setDetailErrors(true);
    }

    /**
     * This method will be called by Olingo on every request to
     * initialize the ODataJPAContext that will be used. 
     */
    @Override
    public ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException {

        log.info("[I32] >>> initializeODataJPAContext()");
        ODataJPAContext ctx = getoDataJPAContext();
        ODataContext octx = ctx.getoDataContext();
        HttpServletRequest request = (HttpServletRequest)octx.getParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT);
        EntityManager em = (EntityManager)request.getAttribute(JerseyConfig.EntityManagerFilter.EM_REQUEST_ATTRIBUTE);
                
        // Here we're passing the EM that was created by the EntityManagerFilter (see JerseyConfig)
        ctx.setEntityManager(new EntityManagerWrapper(em));
        ctx.setPersistenceUnitName("default");
        // We're managing the EM's lifecycle,so we must inform Olingo that it should not
        // try to manage transactions and/or persistence sessions
        ctx.setContainerManaged(true);                
        return ctx;
    }

    static class EntityManagerWrapper implements EntityManager {

        private EntityManager delegate;

        public void persist(Object entity) {
            log.info("[I68] persist: entity.class=" + entity.getClass()
                .getSimpleName());
            delegate.persist(entity);
            // delegate.flush();
        }

        public <T> T merge(T entity) {
            log.info("[I74] merge: entity.class=" + entity.getClass()
                .getSimpleName());
            return delegate.merge(entity);
        }

        public void remove(Object entity) {
            log.info("[I78] remove: entity.class=" + entity.getClass()
                .getSimpleName());
            delegate.remove(entity);
        }

        public <T> T find(Class<T> entityClass,Object primaryKey) {
            log.info("[1]find");
            return delegate.find(entityClass,primaryKey);
        }

        public <T> T find(Class<T> entityClass,Object primaryKey,Map<String,Object> properties) {
            log.info("[2]find");
            return delegate.find(entityClass,primaryKey,properties);
        }

        public <T> T find(Class<T> entityClass,LockModeType lockMode) {
            log.info("[3]find");
            return delegate.find(entityClass,lockMode);
        }

        public <T> T find(Class<T> entityClass,LockModeType lockMode,Object> properties) {
            log.info("[4]find");
            return delegate.find(entityClass,lockMode,properties);
        }

        public <T> T getReference(Class<T> entityClass,Object primaryKey) {
            log.info("[5]find");
            return delegate.getReference(entityClass,primaryKey);
        }

        public void flush() {
            log.info("[1]flush");
            delegate.flush();
        }

        public void setFlushMode(FlushModeType flushMode) {
            log.info("[1]setflushmode");
            delegate.setFlushMode(flushMode);
        }

        public FlushModeType getFlushMode() {
            log.info("[1]getflushmode");
            return delegate.getFlushMode();
        }

        public void lock(Object entity,LockModeType lockMode) {
            log.info("[1]lock");
            delegate.lock(entity,lockMode);
        }

        public void lock(Object entity,Object> properties) {
            log.info("[2]lock");
            delegate.lock(entity,properties);
        }

        public void refresh(Object entity) {
            log.info("[1]refresh");
            delegate.refresh(entity);
        }

        public void refresh(Object entity,Object> properties) {
            log.info("[2]refresh");
            delegate.refresh(entity,properties);
        }

        public void refresh(Object entity,LockModeType lockMode) {
            log.info("[3]refresh");
            delegate.refresh(entity,lockMode);
        }

        public void refresh(Object entity,Object> properties) {
            log.info("[4]refresh");
            delegate.refresh(entity,properties);
        }

        public void clear() {
            log.info("[1]clear");
            delegate.clear();
        }

        public void detach(Object entity) {
            log.info("[1]detach");
            delegate.detach(entity);
        }

        public boolean contains(Object entity) {
            log.info("[1]contains");
            return delegate.contains(entity);
        }

        public LockModeType getLockMode(Object entity) {
            log.info("[1]getlockmode");
            return delegate.getLockMode(entity);
        }

        public void setProperty(String propertyName,Object value) {
            log.info("[1]setproperty:",propertyName);
            delegate.setProperty(propertyName,value);
        }

        public Map<String,Object> getProperties() {
            log.info("[1]getproperties");
            return delegate.getProperties();
        }

        public Query createquery(String qlString) {
            log.info("[1]createquery:",qlString);
            return delegate.createquery(qlString);
        }

        public <T> TypedQuery<T> createquery(CriteriaQuery<T> criteriaQuery) {
            log.info("[2]createquery");
            return delegate.createquery(criteriaQuery);
        }

        public Query createquery(CriteriaUpdate updateQuery) {
            log.info("[3]createquery");
            return delegate.createquery(updateQuery);
        }

        public Query createquery(CriteriaDelete deleteQuery) {
            log.info("[4]createquery");
            return delegate.createquery(deleteQuery);
        }

        public <T> TypedQuery<T> createquery(String qlString,Class<T> resultClass) {
            log.info("[5]createquery");
            return delegate.createquery(qlString,resultClass);
        }

        public Query createNamedQuery(String name) {
            log.info("[1]creatnamedequery");
            return delegate.createNamedQuery(name);
        }

        public <T> TypedQuery<T> createNamedQuery(String name,Class<T> resultClass) {
            log.info("[2]creatnamedequery");
            return delegate.createNamedQuery(name,resultClass);
        }

        public Query createNativeQuery(String sqlString) {
            log.info("[1]creatnativeequery");
            return delegate.createNativeQuery(sqlString);
        }

        public Query createNativeQuery(String sqlString,Class resultClass) {
            log.info("[2]creatnativeequery");
            return delegate.createNativeQuery(sqlString,resultClass);
        }

        public Query createNativeQuery(String sqlString,String resultSetMapping) {
            log.info("[3]creatnativeequery");
            return delegate.createNativeQuery(sqlString,resultSetMapping);
        }

        public StoredProcedureQuery createNamedStoredProcedureQuery(String name) {
            log.info("[1]creatnamedstoredequery");
            return delegate.createNamedStoredProcedureQuery(name);
        }

        public StoredProcedureQuery createStoredProcedureQuery(String procedureName) {
            log.info("[1]createstoredequery");
            return delegate.createStoredProcedureQuery(procedureName);
        }

        public StoredProcedureQuery createStoredProcedureQuery(String procedureName,Class... resultClasses) {
            log.info("[2]createstoredequery");
            return delegate.createStoredProcedureQuery(procedureName,resultClasses);
        }

        public StoredProcedureQuery createStoredProcedureQuery(String procedureName,String... resultSetMappings) {
            log.info("[2]createstoredequery");
            return delegate.createStoredProcedureQuery(procedureName,resultSetMappings);
        }

        public void joinTransaction() {
            log.info("[1]jointransaction");
            delegate.joinTransaction();
        }

        public boolean isJoinedToTransaction() {
            log.info("[1]isjoinedtoransaction");
            return delegate.isJoinedToTransaction();
        }

        public <T> T unwrap(Class<T> cls) {
            log.info("[1]unwrap");
            return delegate.unwrap(cls);
        }

        public Object getDelegate() {
            log.info("[1]getdelegate");
            return delegate.getDelegate();
        }

        public void close() {
            log.info("[I229] close");
            delegate.close();
        }

        public boolean isopen() {
            boolean isOpen = delegate.isopen();
            log.info("[I236] isOpen: " + isOpen);
            return isOpen;
        }

        public EntityTransaction getTransaction() {
            log.info("[I240] getTransaction()");
            return delegate.getTransaction();
        }

        public EntityManagerFactory getEntityManagerFactory() {
            return delegate.getEntityManagerFactory();
        }

        public CriteriaBuilder getCriteriaBuilder() {
            return delegate.getCriteriaBuilder();
        }

        public metamodel getmetamodel() {
            return delegate.getmetamodel();
        }

        public <T> EntityGraph<T> createEntityGraph(Class<T> roottype) {
            return delegate.createEntityGraph(roottype);
        }

        public EntityGraph<?> createEntityGraph(String graphName) {
            return delegate.createEntityGraph(graphName);
        }

        public EntityGraph<?> getEntityGraph(String graphName) {
            return delegate.getEntityGraph(graphName);
        }

        public <T> List<EntityGraph<? super T>> getEntityGraphs(Class<T> entityClass) {
            return delegate.getEntityGraphs(entityClass);
        }

        public EntityManagerWrapper(EntityManager delegate) {
            this.delegate = delegate;
        }

    }

}

和JeresyConfig.java

package gov.doe.app;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.Path;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;

import org.apache.olingo.odata2.api.ODataServiceFactory;
import org.apache.olingo.odata2.core.rest.ODaTarootLocator;
import org.apache.olingo.odata2.core.rest.app.ODataApplication;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Jersey JAX-RS configuration
 * @author Philippe
 *
 */
@Component
@ApplicationPath("/odata")
public class JerseyConfig extends ResourceConfig {
    
    
    public JerseyConfig(DoeODataJPAServiceFactory serviceFactory,EntityManagerFactory emf) {        
        
        ODataApplication app = new ODataApplication();
        
        app
          .getClasses()
          .forEach( c -> {
              // Avoid using the default RootLocator,as we want
              // a Spring Managed one
              if ( !ODaTarootLocator.class.isAssignableFrom(c)) {
                  register(c);
              }
          });
        
        register(new DoeRootLocator(serviceFactory)); 
        register( new EntityManagerFilter(emf));
    }
    
    /**
     * This filter handles the EntityManager transaction lifecycle.
     * @author Philippe
     *
     */
    @Provider
    public static class EntityManagerFilter implements ContainerRequestFilter,ContainerResponseFilter {

        private static final Logger log = LoggerFactory.getLogger(EntityManagerFilter.class);
        public static final String EM_REQUEST_ATTRIBUTE = EntityManagerFilter.class.getName() + "_ENTITY_MANAGER";

        private final EntityManagerFactory emf;

        @Context
        private HttpServletRequest httpRequest;

        public EntityManagerFilter(EntityManagerFactory emf) {
            this.emf = emf;
        }

        @Override
        public void filter(ContainerRequestContext ctx) throws IOException {
            log.info("[I60] >>> filter");
            EntityManager em = this.emf.createEntityManager();
            httpRequest.setAttribute(EM_REQUEST_ATTRIBUTE,em);

            // Start a new transaction unless we have a simple GET
            try {
                if (!"GET".equalsIgnoreCase(ctx.getmethod())) {
                    em.getTransaction()
                        .begin();
                }                
            } catch (Exception e) {
                //Todo: handle exception
                log.info("[I68] <<< EXCEPTION");
                em.close();
                throw e;
            }
        }

        @Override
        public void filter(ContainerRequestContext requestContext,ContainerResponseContext responseContext) throws IOException {

            log.info("[I68] <<< filter");
            EntityManager em = (EntityManager) httpRequest.getAttribute(EM_REQUEST_ATTRIBUTE);

            try {
                if (!"GET".equalsIgnoreCase(requestContext.getmethod())) {
                    EntityTransaction t = em.getTransaction();
                    if (t.isActive()) {
                        if (!t.getRollbackOnly()) {
                            t.commit();
                        }
                    }
                }
            } catch (Exception e) {
                //Todo: handle exception
                log.info("[I68] <<< EXCEPTION");

                em.close();
                throw e;
            }
            log.info("[I68] <<< CLOSE");
            em.close();
        }

    }

    @Path("/")
    public static class DoeRootLocator extends ODaTarootLocator {

        private DoeODataJPAServiceFactory serviceFactory;

        public DoeRootLocator(DoeODataJPAServiceFactory serviceFactory) {
            this.serviceFactory = serviceFactory;
        }

        @Override
        public ODataServiceFactory getServiceFactory() {
            return this.serviceFactory;
        }

    }

}

其余只是模型。 据我了解,这两个负责将请求转换为odata服务。我可以看到,JerseyConfig.java中的私有类的过滤器功能之一有一个调用关闭EntityManager,我认为这应该关闭连接。不幸的是,它似乎并没有关闭连接。这里缺少我应该实施的东西吗?当它在我们的SAP云平台服务中运行时,似乎发生了泄漏,但是当我在本地计算机上运行时,未出现问题。至少我已经使用每分钟执行一次get请求的脚本在本地计算机上运行了该脚本,但是我的应用程序在6小时后仍然可以正常工作。另一方面,当我在SAP云平台服务上运行应用程序时,所有连接都不可用的时间不到一个小时。有人可以告诉我我在这里做错了什么,以及为什么在本地运行它和在服务上运行之间的区别?是运行应用程序的区别还是代码有问题?

更新:在进一步测试中,似乎GET请求导致了泄漏,但是POST请求被关闭就好了。仍然需要测试DELETE和PUT是否会泄漏。

更新2:看来我最初的评估是错误的。我创建了更多测试,实际上问题是调用并发请求会导致一些无法获取JDBC连接,从而导致泄漏。现在的第一个问题是,当Spring Boot应该能够处理并发连接时,为什么并发连接会出现问题,而出现错误时该如何秒关闭连接?我在EntityManagerFilter中编辑了函数以捕获异常并从那里关闭连接,但是它似乎无法正常工作,而且仍然会泄漏。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)