Technoboy- commented on a change in pull request #1702:  Fix bug: Use 
try-with-resources or close this "Statement" in a "finally" clause.
URL: 
https://github.com/apache/incubator-dolphinscheduler/pull/1702#discussion_r367887772
 
 

 ##########
 File path: 
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/ScriptRunner.java
 ##########
 @@ -243,46 +233,35 @@ private void runScript(Connection conn, Reader reader , 
String dbName) throws IO
                                                || fullLineDelimiter && 
trimmedLine.equals(getDelimiter())) {
                                        command.append(line.substring(0, 
line.lastIndexOf(getDelimiter())));
                                        command.append(" ");
-                                       Statement statement = 
conn.createStatement();
-
                                        sql = 
command.toString().replaceAll("\\{\\{APPDB\\}\\}", dbName);
-                                       boolean hasResults = false;
-                                       logger.info("sql : " + sql);
-                                       if (stopOnError) {
-                                               hasResults = 
statement.execute(sql);
-                                       } else {
-                                               try {
-                                                       statement.execute(sql);
-                                               } catch (SQLException e) {
-                                                       
logger.error(e.getMessage(),e);
-                                                       throw e;
-                                               }
-                                       }
-
-                                       ResultSet rs = statement.getResultSet();
-                                       if (hasResults && rs != null) {
-                                               ResultSetMetaData md = 
rs.getMetaData();
-                                               int cols = md.getColumnCount();
-                                               for (int i = 0; i < cols; i++) {
-                                                       String name = 
md.getColumnLabel(i);
-                                                       logger.info(name + 
"\t");
-                                               }
-                                               logger.info("");
-                                               while (rs.next()) {
-                                                       for (int i = 0; i < 
cols; i++) {
-                                                               String value = 
rs.getString(i);
-                                                               
logger.info(value + "\t");
-                                                       }
-                                                       logger.info("");
-                                               }
-                                       }
+                    logger.info("sql : {}", sql);
+                    
+                    try (Statement statement = conn.createStatement()) {
+                        statement.execute(sql);
+                        try (ResultSet rs = statement.getResultSet()) {
+                            if (stopOnError && rs != null) {
+                                ResultSetMetaData md = rs.getMetaData();
+                                int cols = md.getColumnCount();
+                                for (int i = 0; i < cols; i++) {
+                                    String name = md.getColumnLabel(i);
+                                    logger.info("{} \t", name);
+                                }
+                                logger.info("");
+                                while (rs.next()) {
+                                    for (int i = 0; i < cols; i++) {
+                                        String value = rs.getString(i);
+                                        logger.info("{} \t", value);
+                                    }
+                                    logger.info("");
 
 Review comment:
   remove this line

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to