使用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
是春季管理。在数据源级别设置默认超时:
<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)
我的问题是使用旧版本的query-sql。在最新版本中,timeout可以像这样设置:
SQLQuery<Tuple> select = queryFactory.select(map.all());
select.setStatementOptions(StatementOptions.builder().setQueryTimeout(queryTimeout).build());