Skip to content

Commit b4e037b

Browse files
committed
Improve a logic for closing an external resource
* Close an external resource reliably if occurred exception * Use try-with-resources
1 parent d5f024c commit b4e037b

File tree

5 files changed

+38
-44
lines changed

5 files changed

+38
-44
lines changed

src/main/java/org/apache/ibatis/cache/decorators/SerializedCache.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -91,12 +91,10 @@ public boolean equals(Object obj) {
9191
}
9292

9393
private byte[] serialize(Serializable value) {
94-
try {
95-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
96-
ObjectOutputStream oos = new ObjectOutputStream(bos);
94+
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
95+
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
9796
oos.writeObject(value);
9897
oos.flush();
99-
oos.close();
10098
return bos.toByteArray();
10199
} catch (Exception e) {
102100
throw new CacheException("Error serializing object. Cause: " + e, e);
@@ -105,11 +103,9 @@ private byte[] serialize(Serializable value) {
105103

106104
private Serializable deserialize(byte[] value) {
107105
Serializable result;
108-
try {
109-
ByteArrayInputStream bis = new ByteArrayInputStream(value);
110-
ObjectInputStream ois = new CustomObjectInputStream(bis);
106+
try (ByteArrayInputStream bis = new ByteArrayInputStream(value);
107+
ObjectInputStream ois = new CustomObjectInputStream(bis)) {
111108
result = (Serializable) ois.readObject();
112-
ois.close();
113109
} catch (Exception e) {
114110
throw new CacheException("Error deserializing object. Cause: " + e, e);
115111
}

src/main/java/org/apache/ibatis/datasource/pooled/PooledDataSource.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -523,10 +523,9 @@ protected boolean pingConnection(PooledConnection conn) {
523523
log.debug("Testing connection " + conn.getRealHashCode() + " ...");
524524
}
525525
Connection realConn = conn.getRealConnection();
526-
Statement statement = realConn.createStatement();
527-
ResultSet rs = statement.executeQuery(poolPingQuery);
528-
rs.close();
529-
statement.close();
526+
try (Statement statement = realConn.createStatement()) {
527+
statement.executeQuery(poolPingQuery).close();
528+
}
530529
if (!realConn.getAutoCommit()) {
531530
realConn.rollback();
532531
}

src/main/java/org/apache/ibatis/io/DefaultVFS.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -73,17 +73,17 @@ public List<String> list(URL url, String path) throws IOException {
7373
// Some versions of JBoss VFS might give a JAR stream even if the resource
7474
// referenced by the URL isn't actually a JAR
7575
is = url.openStream();
76-
JarInputStream jarInput = new JarInputStream(is);
77-
if (log.isDebugEnabled()) {
78-
log.debug("Listing " + url);
79-
}
80-
for (JarEntry entry; (entry = jarInput.getNextJarEntry()) != null;) {
76+
try (JarInputStream jarInput = new JarInputStream(is)) {
8177
if (log.isDebugEnabled()) {
82-
log.debug("Jar entry: " + entry.getName());
78+
log.debug("Listing " + url);
79+
}
80+
for (JarEntry entry; (entry = jarInput.getNextJarEntry()) != null; ) {
81+
if (log.isDebugEnabled()) {
82+
log.debug("Jar entry: " + entry.getName());
83+
}
84+
children.add(entry.getName());
8385
}
84-
children.add(entry.getName());
8586
}
86-
jarInput.close();
8787
}
8888
else {
8989
/*

src/main/java/org/apache/ibatis/io/Resources.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -125,9 +125,9 @@ public static InputStream getResourceAsStream(ClassLoader loader, String resourc
125125
*/
126126
public static Properties getResourceAsProperties(String resource) throws IOException {
127127
Properties props = new Properties();
128-
InputStream in = getResourceAsStream(resource);
129-
props.load(in);
130-
in.close();
128+
try (InputStream in = getResourceAsStream(resource)) {
129+
props.load(in);
130+
}
131131
return props;
132132
}
133133

@@ -141,9 +141,9 @@ public static Properties getResourceAsProperties(String resource) throws IOExcep
141141
*/
142142
public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException {
143143
Properties props = new Properties();
144-
InputStream in = getResourceAsStream(loader, resource);
145-
props.load(in);
146-
in.close();
144+
try (InputStream in = getResourceAsStream(loader, resource)) {
145+
props.load(in);
146+
}
147147
return props;
148148
}
149149

@@ -244,9 +244,9 @@ public static Reader getUrlAsReader(String urlString) throws IOException {
244244
*/
245245
public static Properties getUrlAsProperties(String urlString) throws IOException {
246246
Properties props = new Properties();
247-
InputStream in = getUrlAsStream(urlString);
248-
props.load(in);
249-
in.close();
247+
try (InputStream in = getUrlAsStream(urlString)) {
248+
props.load(in);
249+
}
250250
return props;
251251
}
252252

src/main/java/org/apache/ibatis/session/SqlSessionManager.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -352,16 +352,15 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
352352
throw ExceptionUtil.unwrapThrowable(t);
353353
}
354354
} else {
355-
final SqlSession autoSqlSession = openSession();
356-
try {
357-
final Object result = method.invoke(autoSqlSession, args);
358-
autoSqlSession.commit();
359-
return result;
360-
} catch (Throwable t) {
361-
autoSqlSession.rollback();
362-
throw ExceptionUtil.unwrapThrowable(t);
363-
} finally {
364-
autoSqlSession.close();
355+
try (SqlSession autoSqlSession = openSession()) {
356+
try {
357+
final Object result = method.invoke(autoSqlSession, args);
358+
autoSqlSession.commit();
359+
return result;
360+
} catch (Throwable t) {
361+
autoSqlSession.rollback();
362+
throw ExceptionUtil.unwrapThrowable(t);
363+
}
365364
}
366365
}
367366
}

0 commit comments

Comments
 (0)