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_r367888236
##########
File path:
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/ScriptRunner.java
##########
@@ -161,44 +161,34 @@ private void runScript(Connection conn, Reader reader)
throws IOException, SQLEx
|| fullLineDelimiter &&
trimmedLine.equals(getDelimiter())) {
command.append(line.substring(0,
line.lastIndexOf(getDelimiter())));
command.append(" ");
- Statement statement =
conn.createStatement();
- 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();
- 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: {}", command);
+ try (Statement statement = conn.createStatement()) {
+ statement.execute(command.toString());
+ 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);
Review comment:
add more info
----------------------------------------------------------------
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