Query Timeout 적용
spring에서 아래 Timeout 설정이 안되네..
@Transactional(timeout=1)
예전 query실행법을 사용하면 적용이 된다..
@Transactional(readOnly = true, timeout = 1)
public ModelAndView executeQuery(@RequestBody Query query) throws Exception {
HashMap<String, Object> mResult = new HashMap<String, Object>();
java.util.List<Map<String,Object>> rows = new ArrayList<>();
int query_timeout = 10;
List<TypeCode> timeoutCodeValue = commonService.selectCodeList("ddlb.selectCodeList", new TypeCode("query_timeout"));
if (timeoutCodeValue != null && timeoutCodeValue.size() >0) {
query_timeout = Integer.parseInt( timeoutCodeValue.get(0).getCodeName().toString() );
}
query.setSql(org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(query.getSql()));
String sql = query.getSql();
sql = "SELECT aql.* FROM ( " + sql + ") as aql";
if (!query.getIsAll()) {
query.setPageFechNo((query.getPageNo() - 1) * query.getPageSize());
sql = sql + "\nlimit " + query.getPageSize() + " offset " + query.getPageFechNo() ;
}
Connection con = DriverManager.getConnection(jdbcUrl,username,password);
Statement stmt = con.createStatement();
ResultSet rs = null;
query.setRecordsTotal(0);
try {
stmt.setQueryTimeout(query_timeout);
// 건수 조회
rs = stmt.executeQuery("SELECT COUNT(*) FROM ("+query.getSql()+")");
rs.next();
int resultCnt = rs.getInt(1);
query.setRecordsFiltered(resultCnt);
query.setRecordsTotal(resultCnt);
if (resultCnt > 100 && query.getIsAll() && !query.getIsReconfirm()) {
logger.warn("====================== resultCnt : " + resultCnt);
return CommonUtil.getResJsonView(null, query);
}
// 목록 조회
rs = stmt.executeQuery(sql);
ResultSetMetaData metaData = rs.getMetaData();
int columnSize = metaData.getColumnCount();
while(rs.next()) {
Map<String, Object> row = new LinkedHashMap<>();
for(int i=1;i<=columnSize;i++) {
String columnName = metaData.getColumnName(i);
Object value = rs.getObject(columnName);
if(value instanceof Long) {
int numValue = (int) (long) value;
row.put(columnName, numValue);
} else if(value instanceof Timestamp) {
String dateValue = value.toString();
dateValue = dateValue.substring(0, dateValue.length()-2);
row.put(columnName, dateValue);
} else {
row.put(columnName, value);
}
}
rows.add(row);
}
} catch(Exception e) {
logger.debug(e.getMessage());
String errMsg = e.getMessage();
if (errMsg.equals("ERROR: canceling statement due to user request")) {
errMsg += "\nQueryTimeout 발생";
}
throw new GlobalException(errMsg, true);
} finally {
try {
if (stmt != null && !stmt.isClosed()) {
stmt.close();
}
if (con != null) con.close();
} catch (SQLException e) {
logger.debug(e.getMessage());
throw e;
}
}
return CommonUtil.getResJsonView(rows, query);
}