Browse Source

fix ResultSet not close and reformat code (#2183)

* fix ResultSet not close

* fix ResultSet not close

* extract code of closing resource to a method

* remove redundant if condition

* modify e2e timeout
pull/3/MERGE
tswstarplanet 4 years ago committed by gaojun2048
parent
commit
1d4cad7913
  1. 60
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/TaskRecordDao.java
  2. 8
      e2e/src/test/resources/config/config.properties

60
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/TaskRecordDao.java

@ -58,13 +58,16 @@ public class TaskRecordDao {
/** /**
* get task record flag * get task record flag
*
* @return whether startup taskrecord * @return whether startup taskrecord
*/ */
public static boolean getTaskRecordFlag() { public static boolean getTaskRecordFlag() {
return conf.getBoolean(Constants.TASK_RECORD_FLAG); return conf.getBoolean(Constants.TASK_RECORD_FLAG);
} }
/** /**
* create connection * create connection
*
* @return connection * @return connection
*/ */
private static Connection getConn() { private static Connection getConn() {
@ -90,6 +93,7 @@ public class TaskRecordDao {
/** /**
* generate where sql string * generate where sql string
*
* @param filterMap filterMap * @param filterMap filterMap
* @return sql string * @return sql string
*/ */
@ -139,6 +143,7 @@ public class TaskRecordDao {
/** /**
* count task record * count task record
*
* @param filterMap filterMap * @param filterMap filterMap
* @param table table * @param table table
* @return task record count * @return task record count
@ -148,6 +153,7 @@ public class TaskRecordDao {
int count = 0; int count = 0;
Connection conn = null; Connection conn = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
conn = getConn(); conn = getConn();
if (conn == null) { if (conn == null) {
@ -156,7 +162,7 @@ public class TaskRecordDao {
String sql = String.format("select count(1) as count from %s", table); String sql = String.format("select count(1) as count from %s", table);
sql += getWhereString(filterMap); sql += getWhereString(filterMap);
pstmt = conn.prepareStatement(sql); pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
count = rs.getInt("count"); count = rs.getInt("count");
break; break;
@ -164,22 +170,14 @@ public class TaskRecordDao {
} catch (SQLException e) { } catch (SQLException e) {
logger.error("Exception ", e); logger.error("Exception ", e);
} finally { } finally {
try { closeResource(rs, pstmt, conn);
if(pstmt != null) {
pstmt.close();
}
if(conn != null){
conn.close();
}
} catch (SQLException e) {
logger.error("Exception ", e);
}
} }
return count; return count;
} }
/** /**
* query task record by filter map paging * query task record by filter map paging
*
* @param filterMap filterMap * @param filterMap filterMap
* @param table table * @param table table
* @return task record list * @return task record list
@ -204,6 +202,7 @@ public class TaskRecordDao {
/** /**
* convert result set to task record * convert result set to task record
*
* @param resultSet resultSet * @param resultSet resultSet
* @return task record * @return task record
* @throws SQLException if error throws SQLException * @throws SQLException if error throws SQLException
@ -232,6 +231,7 @@ public class TaskRecordDao {
/** /**
* query task list by select sql * query task list by select sql
*
* @param selectSql select sql * @param selectSql select sql
* @return task record list * @return task record list
*/ */
@ -239,13 +239,14 @@ public class TaskRecordDao {
List<TaskRecord> recordList = new ArrayList<>(); List<TaskRecord> recordList = new ArrayList<>();
Connection conn = null; Connection conn = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
conn = getConn(); conn = getConn();
if (conn == null) { if (conn == null) {
return recordList; return recordList;
} }
pstmt = conn.prepareStatement(selectSql); pstmt = conn.prepareStatement(selectSql);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
TaskRecord taskRecord = convertToTaskRecord(rs); TaskRecord taskRecord = convertToTaskRecord(rs);
@ -254,22 +255,14 @@ public class TaskRecordDao {
} catch (SQLException e) { } catch (SQLException e) {
logger.error("Exception ", e); logger.error("Exception ", e);
} finally { } finally {
try { closeResource(rs, pstmt, conn);
if(pstmt != null) {
pstmt.close();
}
if(conn != null){
conn.close();
}
} catch (SQLException e) {
logger.error("Exception ", e);
}
} }
return recordList; return recordList;
} }
/** /**
* according to procname and procdate query task record * according to procname and procdate query task record
*
* @param procName procName * @param procName procName
* @param procDate procDate * @param procDate procDate
* @return task record status * @return task record status
@ -300,4 +293,27 @@ public class TaskRecordDao {
} }
} }
private static void closeResource(ResultSet rs, PreparedStatement pstmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
logger.error("Exception ", e);
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
logger.error("Exception ", e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
logger.error("Exception ", e);
}
}
}
} }

8
e2e/src/test/resources/config/config.properties

@ -27,13 +27,13 @@ PASSWORD=dolphinscheduler123
# driver path # driver path
driver.chromeDriver=/usr/local/bin/chromedriver driver.chromeDriver=/usr/local/bin/chromedriver
# implicitly wait(s) # implicitly wait(s)
driver.timeouts.implicitlyWait=10 driver.timeouts.implicitlyWait=30
# show wait(s) # show wait(s)
driver.timeouts.webDriverWait=10 driver.timeouts.webDriverWait=30
# page load timeout(s) # page load timeout(s)
driver.timeouts.pageLoadTimeout=10 driver.timeouts.pageLoadTimeout=30
# JS wait timeouts(s) # JS wait timeouts(s)
driver.timeouts.setScriptTimeout=10 driver.timeouts.setScriptTimeout=30
############### redis ############## ############### redis ##############

Loading…
Cancel
Save