make use of try-with-resources statement

上级 969f44a9
......@@ -322,14 +322,11 @@ public class RunScript extends Tool {
boolean continueOnError) throws SQLException {
try {
org.h2.Driver.load();
Connection conn = DriverManager.getConnection(url, user, password);
if (charset == null) {
charset = StandardCharsets.UTF_8;
}
try {
try (Connection conn = DriverManager.getConnection(url, user, password)) {
process(conn, fileName, continueOnError, charset);
} finally {
conn.close();
}
} catch (IOException e) {
throw DbException.convertIOException(e, fileName);
......
......@@ -133,10 +133,9 @@ public class ValueLobDb extends Value implements Value.ValueClob,
this.fileName = createTempLobFileName(handler);
this.tempFile = this.handler.openFile(fileName, "rw", false);
this.tempFile.autoDelete();
FileStoreOutputStream out = new FileStoreOutputStream(tempFile, null, null);
long tmpPrecision = 0;
boolean compress = this.handler.getLobCompressionAlgorithm(Value.BLOB) != null;
try {
try (FileStoreOutputStream out = new FileStoreOutputStream(tempFile, null, null)) {
while (true) {
tmpPrecision += len;
out.write(buff, 0, len);
......@@ -150,8 +149,6 @@ public class ValueLobDb extends Value implements Value.ValueClob,
break;
}
}
} finally {
out.close();
}
this.precision = tmpPrecision;
this.tableId = 0;
......
......@@ -286,22 +286,17 @@ public class TestMergeUsing extends TestBase implements Trigger {
String gatherResultsSQL, String expectedResultsSQL,
int expectedRowUpdateCount) throws Exception {
deleteDb("mergeUsingQueries");
Connection conn = getConnection("mergeUsingQueries");
Statement stat;
PreparedStatement prep;
ResultSet rs;
int rowCountUpdate;
try {
stat = conn.createStatement();
try (Connection conn = getConnection("mergeUsingQueries")) {
Statement stat = conn.createStatement();
stat.execute(setupSQL);
prep = conn.prepareStatement(statementUnderTest);
rowCountUpdate = prep.executeUpdate();
PreparedStatement prep = conn.prepareStatement(statementUnderTest);
int rowCountUpdate = prep.executeUpdate();
// compare actual results from SQL result set with expected results
// - by diffing (aka set MINUS operation)
rs = stat.executeQuery("( " + gatherResultsSQL + " ) MINUS ( "
ResultSet rs = stat.executeQuery("( " + gatherResultsSQL + " ) MINUS ( "
+ expectedResultsSQL + " )");
int rowCount = 0;
......@@ -319,7 +314,6 @@ public class TestMergeUsing extends TestBase implements Trigger {
assertEquals("Expected update counts differ",
expectedRowUpdateCount, rowCountUpdate);
} finally {
conn.close();
deleteDb("mergeUsingQueries");
}
}
......
......@@ -877,15 +877,12 @@ public class TestSpatial extends TestBase {
"SELECT null, CONCAT('POINT(',A.X,' ',B.X,')')::geometry the_geom " +
"from system_range(0,120) A,system_range(0,10) B;");
stat.execute("create spatial index on pt_cloud(the_geom);");
ResultSet rs = stat.executeQuery(
try (ResultSet rs = stat.executeQuery(
"explain select * from PT_CLOUD " +
"where the_geom && 'POINT(1 1)'");
try {
"where the_geom && 'POINT(1 1)'")) {
assertTrue(rs.next());
assertFalse("H2 should use spatial index got this explain:\n" +
rs.getString(1), rs.getString(1).contains("tableScan"));
} finally {
rs.close();
}
}
deleteDb("spatial");
......
......@@ -102,8 +102,7 @@ public class TestBtreeIndex extends TestBase {
}
String prefix = buff.toString().substring(0, prefixLength);
DeleteDbFiles.execute(getBaseDir() + "/" + getTestName(), null, true);
Connection conn = getConnection(getTestName());
try {
try (Connection conn = getConnection(getTestName())) {
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE a(text VARCHAR PRIMARY KEY)");
PreparedStatement prepInsert = conn.prepareStatement(
......@@ -189,8 +188,6 @@ public class TestBtreeIndex extends TestBase {
if (rs.next()) {
printError(seed, "testCount:" + testCount + " " + rs.getString(1));
}
} finally {
conn.close();
}
deleteDb(getTestName());
}
......
......@@ -121,8 +121,7 @@ public class TestValue extends TestBase {
}
private void testBinaryAndUuid() throws SQLException {
Connection conn = getConnection("binaryAndUuid");
try {
try (Connection conn = getConnection("binaryAndUuid")) {
UUID uuid = UUID.randomUUID();
PreparedStatement prep;
ResultSet rs;
......@@ -139,7 +138,6 @@ public class TestValue extends TestBase {
rs.next();
assertEquals(uuid, rs.getObject(1));
} finally {
conn.close();
deleteDb("binaryAndUuid");
}
}
......@@ -356,13 +354,11 @@ public class TestValue extends TestBase {
}
private void testModulusOperator() throws SQLException {
Connection conn = getConnection("modulus");
try {
try (Connection conn = getConnection("modulus")) {
ResultSet rs = conn.createStatement().executeQuery("CALL 12 % 10");
rs.next();
assertEquals(2, rs.getInt(1));
} finally {
conn.close();
deleteDb("modulus");
}
}
......
......@@ -100,17 +100,14 @@ public class SwitchSource {
}
private void processFile(File f) throws IOException {
RandomAccessFile read = new RandomAccessFile(f, "r");
byte[] buffer;
try {
try (RandomAccessFile read = new RandomAccessFile(f, "r")) {
long len = read.length();
if (len >= Integer.MAX_VALUE) {
throw new IOException("Files bigger than Integer.MAX_VALUE are not supported");
}
buffer = new byte[(int) len];
read.readFully(buffer);
} finally {
read.close();
}
boolean found = false;
// check for ## without creating a string
......
......@@ -79,9 +79,8 @@ public class PropertiesToUTF8 {
if (!new File(source).exists()) {
return;
}
LineNumberReader reader = new LineNumberReader(new InputStreamReader(
new FileInputStream(source), StandardCharsets.UTF_8));
try {
try (LineNumberReader reader = new LineNumberReader(new InputStreamReader(
new FileInputStream(source), StandardCharsets.UTF_8))) {
SortedProperties prop = new SortedProperties();
StringBuilder buff = new StringBuilder();
String key = null;
......@@ -113,8 +112,6 @@ public class PropertiesToUTF8 {
prop.setProperty(key, buff.toString());
}
prop.store(target);
} finally {
reader.close();
}
}
......
......@@ -101,8 +101,7 @@ public class ArchiveToolStore {
buff.clear();
buff.flip();
ArrayList<Integer> posList = new ArrayList<>();
FileChannel fc = FileUtils.open(s, "r");
try {
try (FileChannel fc = FileUtils.open(s, "r")) {
boolean eof = false;
while (true) {
while (!eof && buff.remaining() < 512 * 1024) {
......@@ -153,8 +152,6 @@ public class ArchiveToolStore {
}
printProgress(0, 50, currentSize, totalSize);
}
} finally {
fc.close();
}
int[] posArray = new int[posList.size()];
for (int i = 0; i < posList.size(); i++) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论