Spring boot hikari JDBCConnectionException:无法获取JDBC连接

时间:2018-05-31 10:39:17

标签: hibernate jpa spring-data-jpa entitymanager hikaricp

这是我的 BaseDao.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import java.io.Serializable;

public abstract class BaseDAO<E extends Serializable, K extends Serializable> {

    protected final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    private final Class<E> entityType;
    private final Class<K> keyType;
    @Autowired
    private EntityManagerFactory emf;

    public BaseDAO(final Class<E> entityType, final Class<K> keyType) {
        this.entityType = entityType;
        this.keyType = keyType;
    }

    //<editor-fold desc="BaseDAO properties">
    protected Class<E> getEntityType() {
        return this.entityType;
    }

    protected Class<K> getKeyType() {
        return this.keyType;
    }

    protected EntityManager em() {
        return emf.createEntityManager();
    }

    protected CriteriaBuilder cb() {
        return emf.createEntityManager().getCriteriaBuilder();
    }
}

StudentDao .java

import demo.eaze.school.common.api.entity.StudentEntity;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.List;


@Repository
public class StudentDao extends BaseDAO<StudentEntity, Integer> {
    public StudentDao() {
        super(StudentEntity.class, Integer.class);
    }


    @Transactional(readOnly = true)
    public List<StudentEntity> findStudentSearchData() {
        CriteriaQuery<StudentEntity> query = cb().createQuery(StudentEntity.class);
        Root<StudentEntity> from = query.from(StudentEntity.class);
        CriteriaQuery<StudentEntity> select = query.select(from);
        TypedQuery<StudentEntity> studentEntityTypedQuery = em().createQuery(select).setMaxResults(50);
        return studentEntityTypedQuery.getResultList();
    }


}

当我同时点击10次API时,我开始获得异常

16:04:27.172 [HikariPool-1 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Pool stats (total=10, active=10, idle=0, waiting=3)
16:04:36.330 [HikariPool-2 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-2 - Pool stats (total=10, active=0, idle=10, waiting=0)
16:04:36.361 [HikariPool-3 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-3 - Pool stats (total=10, active=0, idle=10, waiting=0)



DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Timeout failure stats (total=10, active=10, idle=0, waiting=2)
16:04:44.907 [reactor-http-nio-3] DEBUG org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unable to acquire JDBC Connection [n/a]
java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30014ms.
    at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:669)
    at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:183)
    at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:148)
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:100)
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122)
    at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:35)
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:106)
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:136)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:47)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:145)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:171)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:147)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1985)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1915)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1893)
    at org.hibernate.loader.Loader.doQuery(Loader.java:938)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
    at org.hibernate.loader.Loader.doList(Loader.java:2692)
    at org.hibernate.loader.Loader.doList(Loader.java:2675)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507)
    at org.hibernate.loader.Loader.list(Loader.java:2502)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:502)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:392)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1490)
    at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1445)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414)
    at org.hibernate.query.Query.getResultList(Query.java:146)
    at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:72)
    at demo.eaze.school.common.api.dao.StudentDao.findStudentSearchData(StudentDao.java:26)
    at demo.eaze.school.common.api.dao.StudentDao$$FastClassBySpringCGLIB$$9548950c.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at demo.eaze.school.common.api.dao.StudentDao$$EnhancerBySpringCGLIB$$3f35fe59.findStudentSearchData(<generated>)
    at demo.eaze.school.controller.StudentController.students(StudentController.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)

注意:

另一方面,如果我使用Spring的内部JpaReository界面,我没有遇到这个问题。

import demo.eaze.school.common.api.entity.StudentEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface StudentRepository extends JpaRepository<StudentEntity, Long> {
    List<StudentEntity> findByEmail(String username);

    Optional<StudentEntity> findByAdmissionNo(String admissionNumber);
}

所以我的问题是我如何摆脱异常,哪个部分需要配置好以及这是什么原因。

0 个答案:

没有答案