提交 4e38942e authored 作者: LaughingMan's avatar LaughingMan

Use try-with for code cleanup

上级 4dd94a57
...@@ -100,10 +100,6 @@ public class CreateCluster extends Tool { ...@@ -100,10 +100,6 @@ public class CreateCluster extends Tool {
private static void process(String urlSource, String urlTarget, private static void process(String urlSource, String urlTarget,
String user, String password, String serverList) throws SQLException { String user, String password, String serverList) throws SQLException {
Connection connSource = null;
Statement statSource = null;
PipedReader pipeReader = null;
org.h2.Driver.load(); org.h2.Driver.load();
// verify that the database doesn't exist, // verify that the database doesn't exist,
...@@ -130,20 +126,17 @@ public class CreateCluster extends Tool { ...@@ -130,20 +126,17 @@ public class CreateCluster extends Tool {
urlTarget); urlTarget);
} }
try { try (Connection connSource = DriverManager.getConnection(
// use cluster='' so connecting is possible // use cluster='' so connecting is possible
// even if the cluster is enabled // even if the cluster is enabled
connSource = DriverManager.getConnection(urlSource + urlSource + ";CLUSTER=''", user, password);
";CLUSTER=''", user, password); Statement statSource = connSource.createStatement())
statSource = connSource.createStatement(); {
// enable the exclusive mode and close other connections, // enable the exclusive mode and close other connections,
// so that data can't change while restoring the second database // so that data can't change while restoring the second database
statSource.execute("SET EXCLUSIVE 2"); statSource.execute("SET EXCLUSIVE 2");
pipeReader = new PipedReader(); try (PipedReader pipeReader = new PipedReader()) {
try {
/* /*
* Pipe writer is used + closed in the inner class, in a * Pipe writer is used + closed in the inner class, in a
* separate thread (needs to be final). It should be initialized * separate thread (needs to be final). It should be initialized
...@@ -203,10 +196,6 @@ public class CreateCluster extends Tool { ...@@ -203,10 +196,6 @@ public class CreateCluster extends Tool {
// switch back to the regular mode // switch back to the regular mode
statSource.execute("SET EXCLUSIVE FALSE"); statSource.execute("SET EXCLUSIVE FALSE");
} }
} finally {
IOUtils.closeSilently(pipeReader);
JdbcUtils.closeSilently(statSource);
JdbcUtils.closeSilently(connSource);
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论