如何为 SQLQuery 设置超时?

How can i set timeout for SQLQuery?

使用 querydsl-sql 创建 SQLQuery 的方法:

 protected final <T> T select(RelationalPathBase path, Function<SQLQuery, T> code) throws SQLException {
    try (final Connection con = dataSource.getConnection()) {
        return code.apply(new SQLQuery(con, SQLServer2008Templates.builder().printSchema().build()).from(getTable(path)));
    }
}

使用查询的方法:

public List<Tuple> selectDataForProcess() throws SQLException {
    return select(map, sqlQuery -> sqlQuery
            .limit(sendSelectBatch)
            .where(map.selectedOn.isNull())
            .list(map.all()));

}

如何设置查询超时?

根据评论,我认为 datasource 是 spring 管理的。在数据源级别设置默认超时:

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    <!-- Sets default timeout to 5 seconds -->
    <property name="defaultTimeout" value="5" />
</bean>

或者将单个查询包装在事务中并设置超时:

@Transactional(timeout=5)

我的问题是使用旧版本的查询-sql。 在最新版本中,超时可以这样设置:

  SQLQuery<Tuple> select = queryFactory.select(map.all());
    select.setStatementOptions(StatementOptions.builder().setQueryTimeout(queryTimeout).build());