make use of try-with-resources statement

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