java - 如何为 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
是春季管理的。在数据源级别设置默认超时:
<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)
推荐阅读
- rust - 当只需要部分时,如何避免克隆整个大型结构以发送到线程?
- r - R flextable垂直合并以另一列为条件
- python - 如何在python中获取div标签的title属性的文本值?
- python - 如何仅显示一个变量的数据与另一个变量的列数据匹配的行
- git - find命令后如何执行一系列命令?
- swift - 在图表库 Swift 中截断 PieChart 的值标签
- mysql - 使用嵌套查询是否会通过简单查询向 sql 查询添加语义
- python - Python:如何在忽略列表项顺序的情况下比较两个带有列表的字典?
- c - 为什么程序省略了一些行?(加密文本的c程序)
- flutter - Flutter 改变音频