Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i set timeout for SQLQuery?

Tags:

java

querydsl

Method creating SQLQuery using querydsl-sql :

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

Method using query:

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

}

How can i set timeout for query ?

like image 988
c0ld Avatar asked Oct 20 '25 15:10

c0ld


1 Answers

From the comments, I believe datasource is spring managed. Either set the default timeout on the datasource level:

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

Or wrap an individual query in a transaction and set the timeout:

@Transactional(timeout=5)
like image 115
Robert Bain Avatar answered Oct 23 '25 04:10

Robert Bain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!