Browse Source

Fix bug: Use try-with-resources or close this "Statement" in a "finally" clause. (#1702)

* #1701  Fix bug: Use try-with-resources or close this "Statement" in a "finally" clause.

* fix some indent

* refix code smell
pull/2/head
Jave-Chen 4 years ago committed by GitHub
parent
commit
b3b075d915
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 148
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/ScriptRunner.java

148
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/ScriptRunner.java

@ -16,13 +16,17 @@
*/ */
package org.apache.dolphinscheduler.common.utils; package org.apache.dolphinscheduler.common.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.LineNumberReader; import java.io.LineNumberReader;
import java.io.Reader; import java.io.Reader;
import java.sql.*; import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/* /*
* Slightly modified version of the com.ibatis.common.jdbc.ScriptRunner class * Slightly modified version of the com.ibatis.common.jdbc.ScriptRunner class
@ -94,9 +98,7 @@ public class ScriptRunner {
} finally { } finally {
connection.setAutoCommit(originalAutoCommit); connection.setAutoCommit(originalAutoCommit);
} }
} catch (IOException e) { } catch (IOException | SQLException e) {
throw e;
} catch (SQLException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Error running script. Cause: " + e, e); throw new RuntimeException("Error running script. Cause: " + e, e);
@ -114,9 +116,7 @@ public class ScriptRunner {
} finally { } finally {
connection.setAutoCommit(originalAutoCommit); connection.setAutoCommit(originalAutoCommit);
} }
} catch (IOException e) { } catch (IOException | SQLException e) {
throw e;
} catch (SQLException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Error running script. Cause: " + e, e); throw new RuntimeException("Error running script. Cause: " + e, e);
@ -161,44 +161,34 @@ public class ScriptRunner {
|| fullLineDelimiter && trimmedLine.equals(getDelimiter())) { || fullLineDelimiter && trimmedLine.equals(getDelimiter())) {
command.append(line.substring(0, line.lastIndexOf(getDelimiter()))); command.append(line.substring(0, line.lastIndexOf(getDelimiter())));
command.append(" "); command.append(" ");
Statement statement = conn.createStatement(); logger.info("sql: {}", command);
boolean hasResults = false;
logger.info("sql:"+command.toString());
if (stopOnError) {
hasResults = statement.execute(command.toString());
} else {
try {
statement.execute(command.toString());
} catch (SQLException e) {
logger.error(e.getMessage(),e);
throw e;
}
}
ResultSet rs = statement.getResultSet(); try (Statement statement = conn.createStatement()) {
if (hasResults && rs != null) { statement.execute(command.toString());
ResultSetMetaData md = rs.getMetaData(); try (ResultSet rs = statement.getResultSet()) {
int cols = md.getColumnCount(); if (stopOnError && rs != null) {
for (int i = 0; i < cols; i++) { ResultSetMetaData md = rs.getMetaData();
String name = md.getColumnLabel(i); int cols = md.getColumnCount();
logger.info(name + "\t"); for (int i = 0; i < cols; i++) {
} String name = md.getColumnLabel(i);
logger.info(""); logger.info("{} \t", name);
while (rs.next()) { }
for (int i = 0; i < cols; i++) { logger.info("");
String value = rs.getString(i); while (rs.next()) {
logger.info(value + "\t"); for (int i = 0; i < cols; i++) {
} String value = rs.getString(i);
logger.info(""); logger.info("{} \t", value);
} }
} logger.info("");
}
}
}
} catch (SQLException e) {
logger.error("SQLException", e);
throw e;
}
command = null; command = null;
try {
statement.close();
} catch (Exception e) {
// Ignore to workaround a bug in Jakarta DBCP
}
Thread.yield(); Thread.yield();
} else { } else {
command.append(line); command.append(line);
@ -207,11 +197,11 @@ public class ScriptRunner {
} }
} catch (SQLException e) { } catch (SQLException e) {
logger.error("Error executing: " + command.toString()); logger.error("Error executing: {}", command);
throw e; throw e;
} catch (IOException e) { } catch (IOException e) {
e.fillInStackTrace(); e.fillInStackTrace();
logger.error("Error executing: " + command.toString()); logger.error("Error executing: {}", command);
throw e; throw e;
} }
} }
@ -243,46 +233,35 @@ public class ScriptRunner {
|| fullLineDelimiter && trimmedLine.equals(getDelimiter())) { || fullLineDelimiter && trimmedLine.equals(getDelimiter())) {
command.append(line.substring(0, line.lastIndexOf(getDelimiter()))); command.append(line.substring(0, line.lastIndexOf(getDelimiter())));
command.append(" "); command.append(" ");
Statement statement = conn.createStatement();
sql = command.toString().replaceAll("\\{\\{APPDB\\}\\}", dbName); sql = command.toString().replaceAll("\\{\\{APPDB\\}\\}", dbName);
boolean hasResults = false; logger.info("sql : {}", sql);
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(); try (Statement statement = conn.createStatement()) {
if (hasResults && rs != null) { statement.execute(sql);
ResultSetMetaData md = rs.getMetaData(); try (ResultSet rs = statement.getResultSet()) {
int cols = md.getColumnCount(); if (stopOnError && rs != null) {
for (int i = 0; i < cols; i++) { ResultSetMetaData md = rs.getMetaData();
String name = md.getColumnLabel(i); int cols = md.getColumnCount();
logger.info(name + "\t"); for (int i = 0; i < cols; i++) {
} String name = md.getColumnLabel(i);
logger.info(""); logger.info("{} \t", name);
while (rs.next()) { }
for (int i = 0; i < cols; i++) { logger.info("");
String value = rs.getString(i); while (rs.next()) {
logger.info(value + "\t"); for (int i = 0; i < cols; i++) {
} String value = rs.getString(i);
logger.info(""); logger.info("{} \t", value);
} }
} logger.info("");
}
}
}
} catch (SQLException e) {
logger.error("SQLException", e);
throw e;
}
command = null; command = null;
try {
statement.close();
} catch (Exception e) {
// Ignore to workaround a bug in Jakarta DBCP
}
Thread.yield(); Thread.yield();
} else { } else {
command.append(line); command.append(line);
@ -291,11 +270,10 @@ public class ScriptRunner {
} }
} catch (SQLException e) { } catch (SQLException e) {
logger.error("Error executing: " + sql);
throw e; throw e;
} catch (IOException e) { } catch (IOException e) {
e.fillInStackTrace(); e.fillInStackTrace();
logger.error("Error executing: " + sql); logger.error("Error executing: {}", sql);
throw e; throw e;
} }
} }

Loading…
Cancel
Save