选择期间JpaPagingReader请求超时

时间:2019-07-11 18:07:48

标签: java hibernate spring-boot jpa spring-batch

我有春季批处理作业,该作业从通过JpaPagingReader读取表中的所有行开始。 9选择查询后,Spring抛出异常

javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1514) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.query.Query.getResultList(Query.java:135) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1537) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    ... 159 common frames omitted
Caused by: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 10002ms.
    at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:676) ~[HikariCP-3.2.0.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:190) ~[HikariCP-3.2.0.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:155) ~[HikariCP-3.2.0.jar:na]
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:128) ~[HikariCP-3.2.0.jar:na]
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:35) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:106) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    ... 179 common frames omitted

这是我的连接属性

  datasource:
    url: jdbc:postgresql://localhost:5432/kkpostgres
    username: postgres
    password: admin
    driver-class-name: org.postgresql.Driver
    hikari:
      connection-timeout: 10000
      maximum-pool-size: 10
      idle-timeout: 30000
      leak-detection-threshold: 600000

这是我的读者的代码

@Component
public class EmployeeDBReader implements ItemReader<EmployeeEntity> {

    private final EntityManagerFactory entityManagerFactory;

    public EmployeeDBReader(EntityManagerFactory entityManagerFactory) {
        this.entityManagerFactory = entityManagerFactory;
    }

    @Override
    public EmployeeEntity read() throws Exception {
        JpaPagingItemReader<EmployeeEntity> reader = new JpaPagingItemReader<>();
        reader.setEntityManagerFactory(entityManagerFactory);
        reader.setQueryString("SELECT e FROM EmployeeEntity e");
        reader.setTransacted(false);
        reader.open(new ExecutionContext());
        var entity = reader.read();
        return entity;
    }
}

另外,我有一个问题,就是可以使用JpaRepository做相同的阅读器吗?

1 个答案:

答案 0 :(得分:0)

好吧,我找到了更令人满意的方法。我使用了RepositoryItemReader,在我的情况下更好,因为我使用了jpa。代码如下。

@Component
public class EmployeeDBReader implements ItemReader<EmployeeEntity> {

    private static final String QUERY_METHOD_NAME = "findAll";

    private final EmployeeRepository employeeRepository;
    @Setter
    private RepositoryItemReader<EmployeeEntity> repositoryItemReader;

    public EmployeeDBReader(EmployeeRepository employeeRepository) {
        this.employeeRepository = employeeRepository;
        instantiateItemReader();
    }

    private void instantiateItemReader() {
        setRepositoryItemReader(new RepositoryItemReader<>());
        repositoryItemReader.setRepository(employeeRepository);
        repositoryItemReader.setMethodName(QUERY_METHOD_NAME);
        repositoryItemReader.setMethodName("findAll");
        Map<String, Sort.Direction> sortMap = new HashMap<>();
        sortMap.put("id", Sort.Direction.ASC);
        repositoryItemReader.setSort(sortMap);
        repositoryItemReader.setPageSize(1);
        repositoryItemReader.setSaveState(true);
    }

    @Override
    public EmployeeEntity read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        repositoryItemReader.open(new ExecutionContext());
        return repositoryItemReader.read();
    }
}