You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by ke...@apache.org on 2022/08/20 10:50:35 UTC

[dolphinscheduler] branch leak created (now 2da05a8c0b)

This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a change to branch leak
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git


      at 2da05a8c0b Fix some resource leak

This branch includes the following new commits:

     new 2da05a8c0b Fix some resource leak

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[dolphinscheduler] 01/01: Fix some resource leak

Posted by ke...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch leak
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git

commit 2da05a8c0b93202bd7a9fba1aabb0b9ad5aae1eb
Author: kezhenxu94 <ke...@apache.org>
AuthorDate: Sat Aug 20 18:50:17 2022 +0800

    Fix some resource leak
---
 .../dolphinscheduler/api/utils/FileUtils.java      |  5 +--
 .../dolphinscheduler/dao/upgrade/SchemaUtils.java  |  5 +--
 .../tools/datasource/dao/UpgradeDao.java           | 42 +++++++++++-----------
 3 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/FileUtils.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/FileUtils.java
index 606dc6c396..3304d0c497 100644
--- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/FileUtils.java
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/FileUtils.java
@@ -20,6 +20,7 @@ import org.apache.commons.io.IOUtils;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Path;
@@ -76,8 +77,8 @@ public class FileUtils {
      * @return file content string
      */
     public static String file2String(MultipartFile file) {
-        try {
-            return IOUtils.toString(file.getInputStream(), StandardCharsets.UTF_8);
+        try (InputStream inputStream = file.getInputStream()) {
+            return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
         } catch (IOException e) {
             logger.error("file convert to string failed: {}", file.getName());
         }
diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/SchemaUtils.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/SchemaUtils.java
index 625c3551f1..f0fdeeea56 100644
--- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/SchemaUtils.java
+++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/upgrade/SchemaUtils.java
@@ -22,6 +22,7 @@ import org.apache.dolphinscheduler.common.utils.FileUtils;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -106,8 +107,8 @@ public class SchemaUtils {
     public static String getSoftVersion() throws IOException {
         final ClassPathResource softVersionFile = new ClassPathResource("sql/soft_version");
         String softVersion;
-        try {
-            softVersion = FileUtils.readFile2Str(softVersionFile.getInputStream());
+        try (InputStream inputStream = softVersionFile.getInputStream()) {
+            softVersion = FileUtils.readFile2Str(inputStream);
             softVersion = Strings.nullToEmpty(softVersion).replaceAll("\\s+|\r|\n", "");
         } catch (FileNotFoundException e) {
             logger.error(e.getMessage(), e);
diff --git a/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/dao/UpgradeDao.java b/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/dao/UpgradeDao.java
index 49092a6bdc..c3dbbe9bce 100644
--- a/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/dao/UpgradeDao.java
+++ b/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/dao/UpgradeDao.java
@@ -107,8 +107,9 @@ public abstract class UpgradeDao {
         try (Connection conn = dataSource.getConnection()) {
             // Execute the dolphinscheduler_ddl.sql script to create the table structure of dolphinscheduler
             ScriptRunner initScriptRunner = new ScriptRunner(conn, true, true);
-            Reader initSqlReader = new InputStreamReader(mysqlSQLFilePath.getInputStream());
-            initScriptRunner.runScript(initSqlReader);
+            try (Reader initSqlReader = new InputStreamReader(mysqlSQLFilePath.getInputStream())) {
+                initScriptRunner.runScript(initSqlReader);
+            }
         } catch (Exception e) {
             logger.error(e.getMessage(), e);
             throw new RuntimeException(e.getMessage(), e);
@@ -299,22 +300,23 @@ public abstract class UpgradeDao {
             conn.setAutoCommit(false);
             // Execute the upgraded dolphinscheduler dml
             ScriptRunner scriptRunner = new ScriptRunner(conn, false, true);
-            Reader sqlReader = new InputStreamReader(sqlFilePath.getInputStream());
-            scriptRunner.runScript(sqlReader);
-            if (isExistsTable(T_VERSION_NAME)) {
-                // Change version in the version table to the new version
-                String upgradeSQL = String.format("update %s set version = ?", T_VERSION_NAME);
-                pstmt = conn.prepareStatement(upgradeSQL);
-                pstmt.setString(1, schemaVersion);
-                pstmt.executeUpdate();
-            } else if (isExistsTable(T_NEW_VERSION_NAME)) {
-                // Change version in the version table to the new version
-                String upgradeSQL = String.format("update %s set version = ?", T_NEW_VERSION_NAME);
-                pstmt = conn.prepareStatement(upgradeSQL);
-                pstmt.setString(1, schemaVersion);
-                pstmt.executeUpdate();
+            try (Reader sqlReader = new InputStreamReader(sqlFilePath.getInputStream())) {
+                scriptRunner.runScript(sqlReader);
+                if (isExistsTable(T_VERSION_NAME)) {
+                    // Change version in the version table to the new version
+                    String upgradeSQL = String.format("update %s set version = ?", T_VERSION_NAME);
+                    pstmt = conn.prepareStatement(upgradeSQL);
+                    pstmt.setString(1, schemaVersion);
+                    pstmt.executeUpdate();
+                } else if (isExistsTable(T_NEW_VERSION_NAME)) {
+                    // Change version in the version table to the new version
+                    String upgradeSQL = String.format("update %s set version = ?", T_NEW_VERSION_NAME);
+                    pstmt = conn.prepareStatement(upgradeSQL);
+                    pstmt.setString(1, schemaVersion);
+                    pstmt.executeUpdate();
+                }
+                conn.commit();
             }
-            conn.commit();
         } catch (FileNotFoundException e) {
             try {
                 conn.rollback();
@@ -363,9 +365,9 @@ public abstract class UpgradeDao {
             conn.setAutoCommit(true);
             // Execute the dolphinscheduler ddl.sql for the upgrade
             ScriptRunner scriptRunner = new ScriptRunner(conn, true, true);
-            Reader sqlReader = new InputStreamReader(sqlFilePath.getInputStream());
-            scriptRunner.runScript(sqlReader);
-
+            try (Reader sqlReader = new InputStreamReader(sqlFilePath.getInputStream())) {
+                scriptRunner.runScript(sqlReader);
+            }
         } catch (FileNotFoundException e) {
 
             logger.error(e.getMessage(), e);