There are a few things you need to fix. Opening a connection, running a query to get rs, closing it and closing the connection, everything should be performed in the same function area as possible. from your code, you seem to use the "con" variable as a global variable that could potentially cause the problem. you do not close the stm object. or rs object. this code does not work for too long, even if it has no errors. Your code should look like this:
if (stringUtils.isBlank(sql)){ throw new IllegalArgumentsException ("SQL statement is required"); } Connection con = null; PreparedStatement ps =null; Resultset rs = null; try{ con = getConnection(); ps = con.preparestatement(sql); rs = ps.executeQuery(); processResults(rs); close(rs); close(ps); close(con); }catch (Execption e){ log.Exception ("Error in: {}", sql, e); throw new RuntimeException (e); }finally{ close(rs); close(ps); close(con); }
source share