Skip to content

Commit d33c60d

Browse files
committed
feat: BasicDAOHelper has method to set query timeout
1 parent ce64f1e commit d33c60d

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
- BasicDAOHelper has method to set query timeout
11+
1012
## [8.7.1] - 2025-10-31
1113

1214
### Changed

fj-core/src/main/java/org/fugerit/java/core/db/daogen/BasicDAOHelper.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
@Slf4j
3434
public class BasicDAOHelper<T> implements LogObject {
35-
35+
3636
@Override
3737
public Logger getLogger() {
3838
return log;
@@ -52,12 +52,19 @@ public static String fieldListToString( FieldList fl ) {
5252
return buffer.toString();
5353
}
5454

55-
private DAOContext daoContext;
55+
private DAOContext daoContext;
56+
57+
private Integer queryTimeout;
5658

57-
public BasicDAOHelper( DAOContext daoContext) {
58-
this.daoContext =daoContext;
59+
public BasicDAOHelper( DAOContext daoContext ) {
60+
this( daoContext, null );
5961
}
60-
62+
63+
public BasicDAOHelper(DAOContext daoContext, Integer queryTimeout) {
64+
this.daoContext = daoContext;
65+
this.queryTimeout = queryTimeout;
66+
}
67+
6168
public FieldList newFieldList() {
6269
return new FieldList( new FieldFactory() );
6370
}
@@ -75,7 +82,13 @@ public T loadOneHelper( SelectHelper query, RSExtractor<T> re ) throws DAOExcep
7582
public void loadAllHelper( List<T> l, SelectHelper query, RSExtractor<T> re ) throws DAOException {
7683
this.loadAllHelper( l, query.getQueryContent(), query.getFields(), re );
7784
}
78-
85+
86+
private PreparedStatement prepareStatement( PreparedStatement ps ) throws SQLException {
87+
if ( this.queryTimeout != null ) {
88+
ps.setQueryTimeout( this.queryTimeout );
89+
}
90+
return ps;
91+
}
7992

8093
public void loadAllHelper( List<T> l, String query, FieldList fields, RSExtractor<T> re ) throws DAOException {
8194
long startTime = System.currentTimeMillis();
@@ -86,7 +99,7 @@ public void loadAllHelper( List<T> l, String query, FieldList fields, RSExtracto
8699
log.debug( "queryId:'{}', loadAll RSExtractor : '{}'", queryId, re);
87100
Connection conn = this.daoContext.getConnection();
88101
int i=0;
89-
try ( PreparedStatement ps = conn.prepareStatement( query ) ) {
102+
try ( PreparedStatement ps = this.prepareStatement( conn.prepareStatement( query ) ) ) {
90103
DAOHelper.setAll( queryId, ps, fields , log );
91104
long executeStart = System.currentTimeMillis();
92105
try ( ResultSet rs = ps.executeQuery() ) {
@@ -108,7 +121,7 @@ public void loadAllHelper( List<T> l, String query, FieldList fields, RSExtracto
108121
private int updateWorker( String queryId, FieldList fields, String query , long startTime) throws DAOException {
109122
int res = 0;
110123
Connection conn = this.daoContext.getConnection();
111-
try ( PreparedStatement ps = conn.prepareStatement( query ) ) {
124+
try ( PreparedStatement ps = this.prepareStatement( conn.prepareStatement( query ) ) ) {
112125
DAOHelper.setAll( queryId, ps, fields , log );
113126
long executeStart = System.currentTimeMillis();
114127
res = ps.executeUpdate();

fj-core/src/test/java/test/org/fugerit/java/core/db/dao/daogen/TestSelectHelperUpper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected void testQueryHelperIgnoreCase( String syntax ) {
1717
stm.execute( "SET DATABASE SQL SYNTAX "+syntax+" TRUE" );
1818
}
1919
// SelectHelper setup
20-
BasicDAOHelper<String> daoHelper = new BasicDAOHelper<>(context);
20+
BasicDAOHelper<String> daoHelper = new BasicDAOHelper<>(context, 10);
2121
SelectHelper selectHelper = daoHelper.newSelectHelper( "fugerit.address" );
2222
// select helper is basically a query builder, it is possible to add functions, instead of the simple column name
2323
String column = String.format( "UPPER(%s)" , COL_INFO ); // will add 'UPPER(INFO)'

0 commit comments

Comments
 (0)