Skip to content

Commit 0bc1ffb

Browse files
authored
Merge pull request #2078 from nothingzhl/code_optimization
Code optimization
2 parents 739e1dd + 0678440 commit 0bc1ffb

File tree

4 files changed

+45
-92
lines changed

4 files changed

+45
-92
lines changed

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,7 @@ protected boolean isJar(URL url) {
329329
* @return true, if is jar
330330
*/
331331
protected boolean isJar(URL url, byte[] buffer) {
332-
InputStream is = null;
333-
try {
334-
is = url.openStream();
332+
try (InputStream is = url.openStream()) {
335333
is.read(buffer, 0, JAR_MAGIC.length);
336334
if (Arrays.equals(buffer, JAR_MAGIC)) {
337335
if (log.isDebugEnabled()) {
@@ -341,14 +339,6 @@ protected boolean isJar(URL url, byte[] buffer) {
341339
}
342340
} catch (Exception e) {
343341
// Failure to read the stream means this is not a JAR
344-
} finally {
345-
if (is != null) {
346-
try {
347-
is.close();
348-
} catch (Exception e) {
349-
// Ignore
350-
}
351-
}
352342
}
353343

354344
return false;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
* by calling {@link #setClassLoader(ClassLoader)} prior to invoking any of the {@code find()}
3838
* methods.</p>
3939
*
40-
* <p>General searches are initiated by calling the
41-
* {@link #find(org.apache.ibatis.io.ResolverUtil.Test, String)} ()} method and supplying
40+
* <p>General searches are initiated by calling the {@link #find(Test, String)} and supplying
4241
* a package name and a Test instance. This will cause the named package <b>and all sub-packages</b>
4342
* to be scanned for classes that meet the test. There are also utility methods for the common
4443
* use cases of scanning multiple packages for extensions of particular classes, or classes

src/main/java/org/apache/ibatis/jdbc/ScriptRunner.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ private boolean commandReadyToExecute(String trimmedLine) {
238238
}
239239

240240
private void executeStatement(String command) throws SQLException {
241-
Statement statement = connection.createStatement();
242-
try {
241+
try (Statement statement = connection.createStatement()) {
243242
statement.setEscapeProcessing(escapeProcessing);
244243
String sql = command;
245244
if (removeCRs) {
@@ -262,13 +261,6 @@ private void executeStatement(String command) throws SQLException {
262261
printlnError(message);
263262
}
264263
}
265-
} finally {
266-
try {
267-
statement.close();
268-
} catch (Exception ignored) {
269-
// Ignore to workaround a bug in some connection pools
270-
// (Does anyone know the details of the bug?)
271-
}
272264
}
273265
}
274266

src/main/java/org/apache/ibatis/jdbc/SqlRunner.java

Lines changed: 42 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2019 the original author or authors.
2+
* Copyright 2009-2020 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.
@@ -77,16 +77,10 @@ public Map<String, Object> selectOne(String sql, Object... args) throws SQLExcep
7777
* @throws SQLException If statement preparation or execution fails
7878
*/
7979
public List<Map<String, Object>> selectAll(String sql, Object... args) throws SQLException {
80-
PreparedStatement ps = connection.prepareStatement(sql);
81-
try {
80+
try (PreparedStatement ps = connection.prepareStatement(sql)) {
8281
setParameters(ps, args);
83-
ResultSet rs = ps.executeQuery();
84-
return getResults(rs);
85-
} finally {
86-
try {
87-
ps.close();
88-
} catch (SQLException e) {
89-
//ignore
82+
try (ResultSet rs = ps.executeQuery()) {
83+
return getResults(rs);
9084
}
9185
}
9286
}
@@ -111,17 +105,19 @@ public int insert(String sql, Object... args) throws SQLException {
111105
setParameters(ps, args);
112106
ps.executeUpdate();
113107
if (useGeneratedKeySupport) {
114-
List<Map<String, Object>> keys = getResults(ps.getGeneratedKeys());
115-
if (keys.size() == 1) {
116-
Map<String, Object> key = keys.get(0);
117-
Iterator<Object> i = key.values().iterator();
118-
if (i.hasNext()) {
119-
Object genkey = i.next();
120-
if (genkey != null) {
121-
try {
122-
return Integer.parseInt(genkey.toString());
123-
} catch (NumberFormatException e) {
124-
//ignore, no numeric key support
108+
try (ResultSet generatedKeys = ps.getGeneratedKeys()) {
109+
List<Map<String, Object>> keys = getResults(generatedKeys);
110+
if (keys.size() == 1) {
111+
Map<String, Object> key = keys.get(0);
112+
Iterator<Object> i = key.values().iterator();
113+
if (i.hasNext()) {
114+
Object genkey = i.next();
115+
if (genkey != null) {
116+
try {
117+
return Integer.parseInt(genkey.toString());
118+
} catch (NumberFormatException e) {
119+
//ignore, no numeric key support
120+
}
125121
}
126122
}
127123
}
@@ -146,16 +142,9 @@ public int insert(String sql, Object... args) throws SQLException {
146142
* @throws SQLException If statement preparation or execution fails
147143
*/
148144
public int update(String sql, Object... args) throws SQLException {
149-
PreparedStatement ps = connection.prepareStatement(sql);
150-
try {
145+
try (PreparedStatement ps = connection.prepareStatement(sql)) {
151146
setParameters(ps, args);
152147
return ps.executeUpdate();
153-
} finally {
154-
try {
155-
ps.close();
156-
} catch (SQLException e) {
157-
//ignore
158-
}
159148
}
160149
}
161150

@@ -179,15 +168,8 @@ public int delete(String sql, Object... args) throws SQLException {
179168
* @throws SQLException If statement preparation or execution fails
180169
*/
181170
public void run(String sql) throws SQLException {
182-
Statement stmt = connection.createStatement();
183-
try {
171+
try (Statement stmt = connection.createStatement()) {
184172
stmt.execute(sql);
185-
} finally {
186-
try {
187-
stmt.close();
188-
} catch (SQLException e) {
189-
//ignore
190-
}
191173
}
192174
}
193175

@@ -221,43 +203,33 @@ private void setParameters(PreparedStatement ps, Object... args) throws SQLExcep
221203
}
222204

223205
private List<Map<String, Object>> getResults(ResultSet rs) throws SQLException {
224-
try {
225-
List<Map<String, Object>> list = new ArrayList<>();
226-
List<String> columns = new ArrayList<>();
227-
List<TypeHandler<?>> typeHandlers = new ArrayList<>();
228-
ResultSetMetaData rsmd = rs.getMetaData();
229-
for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
230-
columns.add(rsmd.getColumnLabel(i + 1));
231-
try {
232-
Class<?> type = Resources.classForName(rsmd.getColumnClassName(i + 1));
233-
TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(type);
234-
if (typeHandler == null) {
235-
typeHandler = typeHandlerRegistry.getTypeHandler(Object.class);
236-
}
237-
typeHandlers.add(typeHandler);
238-
} catch (Exception e) {
239-
typeHandlers.add(typeHandlerRegistry.getTypeHandler(Object.class));
240-
}
241-
}
242-
while (rs.next()) {
243-
Map<String, Object> row = new HashMap<>();
244-
for (int i = 0, n = columns.size(); i < n; i++) {
245-
String name = columns.get(i);
246-
TypeHandler<?> handler = typeHandlers.get(i);
247-
row.put(name.toUpperCase(Locale.ENGLISH), handler.getResult(rs, name));
206+
List<Map<String, Object>> list = new ArrayList<>();
207+
List<String> columns = new ArrayList<>();
208+
List<TypeHandler<?>> typeHandlers = new ArrayList<>();
209+
ResultSetMetaData rsmd = rs.getMetaData();
210+
for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
211+
columns.add(rsmd.getColumnLabel(i + 1));
212+
try {
213+
Class<?> type = Resources.classForName(rsmd.getColumnClassName(i + 1));
214+
TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(type);
215+
if (typeHandler == null) {
216+
typeHandler = typeHandlerRegistry.getTypeHandler(Object.class);
248217
}
249-
list.add(row);
218+
typeHandlers.add(typeHandler);
219+
} catch (Exception e) {
220+
typeHandlers.add(typeHandlerRegistry.getTypeHandler(Object.class));
250221
}
251-
return list;
252-
} finally {
253-
if (rs != null) {
254-
try {
255-
rs.close();
256-
} catch (Exception e) {
257-
// ignore
258-
}
222+
}
223+
while (rs.next()) {
224+
Map<String, Object> row = new HashMap<>();
225+
for (int i = 0, n = columns.size(); i < n; i++) {
226+
String name = columns.get(i);
227+
TypeHandler<?> handler = typeHandlers.get(i);
228+
row.put(name.toUpperCase(Locale.ENGLISH), handler.getResult(rs, name));
259229
}
230+
list.add(row);
260231
}
232+
return list;
261233
}
262234

263235
}

0 commit comments

Comments
 (0)