未验证 提交 22d6afa4 编写于 作者: W Wenjun Ruan 提交者: GitHub

Merge pull request #65 from WhaleOps/whaleops/leak

Fix some resource leak (#11576)
...@@ -20,6 +20,7 @@ import org.apache.commons.io.IOUtils; ...@@ -20,6 +20,7 @@ import org.apache.commons.io.IOUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Path; import java.nio.file.Path;
...@@ -76,8 +77,8 @@ public class FileUtils { ...@@ -76,8 +77,8 @@ public class FileUtils {
* @return file content string * @return file content string
*/ */
public static String file2String(MultipartFile file) { public static String file2String(MultipartFile file) {
try { try (InputStream inputStream = file.getInputStream()) {
return IOUtils.toString(file.getInputStream(), StandardCharsets.UTF_8); return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
logger.error("file convert to string failed: {}", file.getName()); logger.error("file convert to string failed: {}", file.getName());
} }
......
...@@ -24,6 +24,7 @@ import org.apache.commons.lang.StringUtils; ...@@ -24,6 +24,7 @@ import org.apache.commons.lang.StringUtils;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
...@@ -108,8 +109,8 @@ public class SchemaUtils { ...@@ -108,8 +109,8 @@ public class SchemaUtils {
public static String getSoftVersion() throws IOException { public static String getSoftVersion() throws IOException {
final ClassPathResource softVersionFile = new ClassPathResource("sql/soft_version"); final ClassPathResource softVersionFile = new ClassPathResource("sql/soft_version");
String softVersion; String softVersion;
try { try (InputStream inputStream = softVersionFile.getInputStream()) {
softVersion = FileUtils.readFile2Str(softVersionFile.getInputStream()); softVersion = FileUtils.readFile2Str(inputStream);
softVersion = Strings.nullToEmpty(softVersion).replaceAll("\\s+|\r|\n", ""); softVersion = Strings.nullToEmpty(softVersion).replaceAll("\\s+|\r|\n", "");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
......
...@@ -106,8 +106,9 @@ public abstract class UpgradeDao { ...@@ -106,8 +106,9 @@ public abstract class UpgradeDao {
try (Connection conn = dataSource.getConnection()) { try (Connection conn = dataSource.getConnection()) {
// Execute the dolphinscheduler_ddl.sql script to create the table structure of dolphinscheduler // Execute the dolphinscheduler_ddl.sql script to create the table structure of dolphinscheduler
ScriptRunner initScriptRunner = new ScriptRunner(conn, true, true); ScriptRunner initScriptRunner = new ScriptRunner(conn, true, true);
Reader initSqlReader = new InputStreamReader(mysqlSQLFilePath.getInputStream()); try (Reader initSqlReader = new InputStreamReader(mysqlSQLFilePath.getInputStream())) {
initScriptRunner.runScript(initSqlReader); initScriptRunner.runScript(initSqlReader);
}
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e); throw new RuntimeException(e.getMessage(), e);
...@@ -298,22 +299,23 @@ public abstract class UpgradeDao { ...@@ -298,22 +299,23 @@ public abstract class UpgradeDao {
conn.setAutoCommit(false); conn.setAutoCommit(false);
// Execute the upgraded dolphinscheduler dml // Execute the upgraded dolphinscheduler dml
ScriptRunner scriptRunner = new ScriptRunner(conn, false, true); ScriptRunner scriptRunner = new ScriptRunner(conn, false, true);
Reader sqlReader = new InputStreamReader(sqlFilePath.getInputStream()); try (Reader sqlReader = new InputStreamReader(sqlFilePath.getInputStream())) {
scriptRunner.runScript(sqlReader); scriptRunner.runScript(sqlReader);
if (isExistsTable(T_VERSION_NAME)) { if (isExistsTable(T_VERSION_NAME)) {
// Change version in the version table to the new version // Change version in the version table to the new version
String upgradeSQL = String.format("update %s set version = ?", T_VERSION_NAME); String upgradeSQL = String.format("update %s set version = ?", T_VERSION_NAME);
pstmt = conn.prepareStatement(upgradeSQL); pstmt = conn.prepareStatement(upgradeSQL);
pstmt.setString(1, schemaVersion); pstmt.setString(1, schemaVersion);
pstmt.executeUpdate(); pstmt.executeUpdate();
} else if (isExistsTable(T_NEW_VERSION_NAME)) { } else if (isExistsTable(T_NEW_VERSION_NAME)) {
// Change version in the version table to the new version // Change version in the version table to the new version
String upgradeSQL = String.format("update %s set version = ?", T_NEW_VERSION_NAME); String upgradeSQL = String.format("update %s set version = ?", T_NEW_VERSION_NAME);
pstmt = conn.prepareStatement(upgradeSQL); pstmt = conn.prepareStatement(upgradeSQL);
pstmt.setString(1, schemaVersion); pstmt.setString(1, schemaVersion);
pstmt.executeUpdate(); pstmt.executeUpdate();
}
conn.commit();
} }
conn.commit();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
try { try {
conn.rollback(); conn.rollback();
...@@ -362,9 +364,9 @@ public abstract class UpgradeDao { ...@@ -362,9 +364,9 @@ public abstract class UpgradeDao {
conn.setAutoCommit(true); conn.setAutoCommit(true);
// Execute the dolphinscheduler ddl.sql for the upgrade // Execute the dolphinscheduler ddl.sql for the upgrade
ScriptRunner scriptRunner = new ScriptRunner(conn, true, true); ScriptRunner scriptRunner = new ScriptRunner(conn, true, true);
Reader sqlReader = new InputStreamReader(sqlFilePath.getInputStream()); try (Reader sqlReader = new InputStreamReader(sqlFilePath.getInputStream())) {
scriptRunner.runScript(sqlReader); scriptRunner.runScript(sqlReader);
}
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册