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 5 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
*
* @return whether startup taskrecord
*/
public static boolean getTaskRecordFlag() {
return conf.getBoolean(Constants.TASK_RECORD_FLAG);
}
/**
* create connection
*
* @return connection
*/
private static Connection getConn() {
@ -90,6 +93,7 @@ public class TaskRecordDao {
/**
* generate where sql string
*
* @param filterMap filterMap
* @return sql string
*/
@ -139,6 +143,7 @@ public class TaskRecordDao {
/**
* count task record
*
* @param filterMap filterMap
* @param table table
* @return task record count
@ -148,6 +153,7 @@ public class TaskRecordDao {
int count = 0;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConn();
if (conn == null) {
@ -156,7 +162,7 @@ public class TaskRecordDao {
String sql = String.format("select count(1) as count from %s", table);
sql += getWhereString(filterMap);
pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
rs = pstmt.executeQuery();
while (rs.next()) {
count = rs.getInt("count");
break;
@ -164,22 +170,14 @@ public class TaskRecordDao {
} catch (SQLException e) {
logger.error("Exception ", e);
} finally {
try {
if(pstmt != null) {
pstmt.close();
}
if(conn != null){
conn.close();
}
} catch (SQLException e) {
logger.error("Exception ", e);
}
closeResource(rs, pstmt, conn);
}
return count;
}
/**
* query task record by filter map paging
*
* @param filterMap filterMap
* @param table table
* @return task record list
@ -204,6 +202,7 @@ public class TaskRecordDao {
/**
* convert result set to task record
*
* @param resultSet resultSet
* @return task record
* @throws SQLException if error throws SQLException
@ -232,6 +231,7 @@ public class TaskRecordDao {
/**
* query task list by select sql
*
* @param selectSql select sql
* @return task record list
*/
@ -239,13 +239,14 @@ public class TaskRecordDao {
List<TaskRecord> recordList = new ArrayList<>();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConn();
if (conn == null) {
return recordList;
}
pstmt = conn.prepareStatement(selectSql);
ResultSet rs = pstmt.executeQuery();
rs = pstmt.executeQuery();
while (rs.next()) {
TaskRecord taskRecord = convertToTaskRecord(rs);
@ -254,22 +255,14 @@ public class TaskRecordDao {
} catch (SQLException e) {
logger.error("Exception ", e);
} finally {
try {
if(pstmt != null) {
pstmt.close();
}
if(conn != null){
conn.close();
}
} catch (SQLException e) {
logger.error("Exception ", e);
}
closeResource(rs, pstmt, conn);
}
return recordList;
}
/**
* according to procname and procdate query task record
*
* @param procName procName
* @param procDate procDate
* @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.chromeDriver=/usr/local/bin/chromedriver
# implicitly wait(s)
driver.timeouts.implicitlyWait=10
driver.timeouts.implicitlyWait=30
# show wait(s)
driver.timeouts.webDriverWait=10
driver.timeouts.webDriverWait=30
# page load timeout(s)
driver.timeouts.pageLoadTimeout=10
driver.timeouts.pageLoadTimeout=30
# JS wait timeouts(s)
driver.timeouts.setScriptTimeout=10
driver.timeouts.setScriptTimeout=30
############### redis ##############

Loading…
Cancel
Save