首页 > 解决方案 > 如何为 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()));

}

如何设置查询超时?

标签: javaquerydsl

解决方案


从评论来看,我相信datasource是春季管理的。在数据源级别设置默认超时:

<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)

推荐阅读