3232 */
3333@ Slf4j
3434public 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 ();
0 commit comments