fix resource leak problems

This commit is contained in:
tushengxia 2020-09-09 21:38:06 +08:00
parent f8a2048422
commit 285bf04592
6 changed files with 40 additions and 41 deletions

View File

@ -238,8 +238,7 @@ public class DataCenterClient
{
String query = "PREPARE subStatement FROM " + sql;
String describe = "DESCRIBE OUTPUT subStatement";
try {
StatementClient preparedClient = execute(query);
try (StatementClient preparedClient = execute(query)) {
DataCenterClientSession newClientSession = DataCenterStatementClientFactory.createClientSession(
preparedClient, this.config, this.typeManager);

View File

@ -144,5 +144,15 @@ public class HBaseGetRecordCursor
}
@Override
public void close() {}
public void close()
{
try (Connection connection = this.conn) {
if (connection != null) {
connection.close();
}
}
catch (IOException e) {
// ignore exception from close
}
}
}

View File

@ -283,6 +283,8 @@ public class HBaseRecordCursor
@Override
public void close()
{
this.scanner.close();
if (this.scanner != null) {
this.scanner.close();
}
}
}

View File

@ -192,14 +192,8 @@ public class FileBasedSeedStore
private void writeToFile(Path file, String content, boolean overwrite)
throws IOException
{
OutputStream os;
if (overwrite) {
os = fs.newOutputStream(file);
try (OutputStream os = (overwrite) ? fs.newOutputStream(file) : fs.newOutputStream(file, CREATE_NEW)) {
os.write(content.getBytes());
}
else {
os = fs.newOutputStream(file, CREATE_NEW);
}
os.write(content.getBytes());
os.close();
}
}

View File

@ -94,7 +94,9 @@ public class KeystoreSecurityKeyManager
{
Path keystorePath = Paths.get(config.getFileStorePath());
String keyStr = "";
try (InputStream inputStream = fileSystemClientManager.getFileSystemClient(SHARE_FS_CLIENT_CONFIG_NAME, Paths.get("/")).newInputStream(keystorePath)) {
try (HetuFileSystemClient hetuFileSystemClient =
fileSystemClientManager.getFileSystemClient(SHARE_FS_CLIENT_CONFIG_NAME, Paths.get("/"));
InputStream inputStream = hetuFileSystemClient.newInputStream(keystorePath)) {
KeyStore keyStore = KeyStore.getInstance(PKCS12);
keyStore.load(inputStream, config.getKeystorePassword().toCharArray());
Key key = keyStore.getKey(keyFileName, config.getKeystorePassword().toCharArray());
@ -143,9 +145,8 @@ public class KeystoreSecurityKeyManager
KeyStore keyStore;
InputStream inputStream = null;
OutputStream outputStream = null;
HetuFileSystemClient hetuFileSystemClient;
try {
hetuFileSystemClient = fileSystemClientManager.getFileSystemClient(SHARE_FS_CLIENT_CONFIG_NAME, Paths.get("/"));
try (HetuFileSystemClient hetuFileSystemClient =
fileSystemClientManager.getFileSystemClient(SHARE_FS_CLIENT_CONFIG_NAME, Paths.get("/"))) {
inputStream = hetuFileSystemClient.newInputStream(keystorPath);
keyStore = KeyStore.getInstance(PKCS12);
keyStore.load(inputStream, config.getKeystorePassword().toCharArray());
@ -167,12 +168,13 @@ public class KeystoreSecurityKeyManager
throw new SecurityKeyException(format("error in I/O: fail to delete alias[%s] from keystore.", keyFileName));
}
finally {
try {
if (inputStream != null) {
inputStream.close();
try (InputStream inputStreamRelease = inputStream;
OutputStream outputStreamRelease = outputStream) {
if (inputStreamRelease != null) {
inputStreamRelease.close();
}
if (outputStream != null) {
outputStream.close();
if (outputStreamRelease != null) {
outputStreamRelease.close();
}
}
catch (IOException e) {
@ -191,9 +193,8 @@ public class KeystoreSecurityKeyManager
InputStream inputStream = null;
OutputStream outputStream = null;
HetuFileSystemClient hetuFileSystemClient;
try {
hetuFileSystemClient = fileSystemClientManager.getFileSystemClient(SHARE_FS_CLIENT_CONFIG_NAME, Paths.get("/"));
try (HetuFileSystemClient hetuFileSystemClient =
fileSystemClientManager.getFileSystemClient(SHARE_FS_CLIENT_CONFIG_NAME, Paths.get("/"))) {
boolean isStoreFileExists = hetuFileSystemClient.exists(keystorPath);
KeyStore keyStore = KeyStore.getInstance(PKCS12);
if (isStoreFileExists) {
@ -223,12 +224,13 @@ public class KeystoreSecurityKeyManager
throw new SecurityKeyException("error in I/O: create file failed.");
}
finally {
try {
if (inputStream != null) {
inputStream.close();
try (InputStream inputStreamRelease = inputStream;
OutputStream outputStreamRelease = outputStream) {
if (inputStreamRelease != null) {
inputStreamRelease.close();
}
if (outputStream != null) {
outputStream.close();
if (outputStreamRelease != null) {
outputStreamRelease.close();
}
}
catch (IOException e) {
@ -240,10 +242,8 @@ public class KeystoreSecurityKeyManager
private void createStoreDirIfNotExists()
{
String file = config.getFileStorePath();
HetuFileSystemClient hetuFileSystemClient;
try {
hetuFileSystemClient = fileSystemClientManager.getFileSystemClient(SHARE_FS_CLIENT_CONFIG_NAME, Paths.get("/"));
try (HetuFileSystemClient hetuFileSystemClient =
fileSystemClientManager.getFileSystemClient(SHARE_FS_CLIENT_CONFIG_NAME, Paths.get("/"))) {
int lastIndex = file.lastIndexOf(File.separator);
String tmpFileDir = file.substring(0, lastIndex);
if (hetuFileSystemClient.exists(Paths.get(tmpFileDir))) {

View File

@ -424,15 +424,9 @@ public class FileBasedLock
private void writeToFile(Path file, String content, boolean overwrite)
throws IOException
{
OutputStream os;
if (overwrite) {
os = fs.newOutputStream(file);
try (OutputStream os = (overwrite) ? fs.newOutputStream(file) : fs.newOutputStream(file, CREATE_NEW)) {
os.write(content.getBytes());
}
else {
os = fs.newOutputStream(file, CREATE_NEW);
}
os.write(content.getBytes());
os.close();
}
private static String checkProperty(Properties properties, String key)