问题:每小时扫描一次SqlServer数据库表,使用PreparedStatement excutequery方法。结果经常出现read timeout的异常。
访问数据库的函数如下
- Java code
public int getTrackingInfo() { int rowcount = 0; DBOperator dbOpr = new DBOperator("sqlserver"); String table = "TrackingEvent_" + SemUtil.getDateTime(new Date(currentTime - 4 * oneHour), "yyyyMM"); String sql = "SELECT count(*) as count FROM " + table + " where evtdatetime>=? and evtdatetime<?"; PreparedStatement ps = dbOpr.getPreparedStatement(sql); ResultSet rs = null; try { ps.setString(1, startTime); ps.setString(2, endTime); rs = ps.executeQuery(); if (rs.next()) { rowcount = rs.getInt("count"); } rs.close(); ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); try { rs.close(); ps.close(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }catch (Exception e){ System.out.println("trackingEvents timeout error"); } return rowcount; }
设置setQueryTimeout无效,求助sqlserver读数据能否这只timeout啊?~~或者是否有其他的解决方法?
------解决方案--------------------
还是改善网络环境,然后把>=改成=吧。