提交 05d1acec authored 作者: LaughingMan's avatar LaughingMan

Limit the scope of two variables by using try-with statements

上级 9ed55eec
...@@ -100,8 +100,8 @@ public class CreateCluster extends Tool { ...@@ -100,8 +100,8 @@ 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, connTarget = null; Connection connSource = null;
Statement statSource = null, statTarget = null; Statement statSource = null;
PipedReader pipeReader = null; PipedReader pipeReader = null;
try { try {
...@@ -110,15 +110,13 @@ public class CreateCluster extends Tool { ...@@ -110,15 +110,13 @@ public class CreateCluster extends Tool {
// verify that the database doesn't exist, // verify that the database doesn't exist,
// or if it exists (an old cluster instance), it is deleted // or if it exists (an old cluster instance), it is deleted
boolean exists = true; boolean exists = true;
try { try (Connection connTarget = DriverManager.getConnection(urlTarget +
connTarget = DriverManager.getConnection(urlTarget + ";IFEXISTS=TRUE;CLUSTER=" + Constants.CLUSTERING_ENABLED,
";IFEXISTS=TRUE;CLUSTER=" + Constants.CLUSTERING_ENABLED, user, password);
user, password); Statement stat = connTarget.createStatement())
Statement stat = connTarget.createStatement(); {
stat.execute("DROP ALL OBJECTS DELETE FILES"); stat.execute("DROP ALL OBJECTS DELETE FILES");
stat.close();
exists = false; exists = false;
connTarget.close();
} catch (SQLException e) { } catch (SQLException e) {
if (e.getErrorCode() == ErrorCode.DATABASE_NOT_FOUND_1) { if (e.getErrorCode() == ErrorCode.DATABASE_NOT_FOUND_1) {
// database does not exists yet - ok // database does not exists yet - ok
...@@ -162,13 +160,12 @@ public class CreateCluster extends Tool { ...@@ -162,13 +160,12 @@ public class CreateCluster extends Tool {
final ResultSet rs = statSource.executeQuery("SCRIPT"); final ResultSet rs = statSource.executeQuery("SCRIPT");
// Delete the target database first. // Delete the target database first.
connTarget = DriverManager.getConnection( try (Connection connTarget = DriverManager.getConnection(
urlTarget + ";CLUSTER=''", user, password); urlTarget + ";CLUSTER=''", user, password);
statTarget = connTarget.createStatement(); Statement statTarget = connTarget.createStatement())
statTarget.execute("DROP ALL OBJECTS DELETE FILES"); {
statTarget.close(); statTarget.execute("DROP ALL OBJECTS DELETE FILES");
connTarget.close(); }
new Thread( new Thread(
new Runnable(){ new Runnable(){
...@@ -190,14 +187,16 @@ public class CreateCluster extends Tool { ...@@ -190,14 +187,16 @@ public class CreateCluster extends Tool {
).start(); ).start();
// Read data from pipe reader, restore on target. // Read data from pipe reader, restore on target.
connTarget = DriverManager.getConnection(urlTarget, user, password); try (Connection connTarget = DriverManager.getConnection(
RunScript.execute(connTarget,pipeReader); urlTarget, user, password);
statTarget = connTarget.createStatement(); Statement statTarget = connTarget.createStatement())
{
// set the cluster to the serverList on both databases RunScript.execute(connTarget,pipeReader);
statSource.executeUpdate("SET CLUSTER '" + serverList + "'");
statTarget.executeUpdate("SET CLUSTER '" + serverList + "'"); // set the cluster to the serverList on both databases
statSource.executeUpdate("SET CLUSTER '" + serverList + "'");
statTarget.executeUpdate("SET CLUSTER '" + serverList + "'");
}
} catch (IOException ex) { } catch (IOException ex) {
throw new SQLException(ex); throw new SQLException(ex);
} finally { } finally {
...@@ -207,9 +206,7 @@ public class CreateCluster extends Tool { ...@@ -207,9 +206,7 @@ public class CreateCluster extends Tool {
} finally { } finally {
IOUtils.closeSilently(pipeReader); IOUtils.closeSilently(pipeReader);
JdbcUtils.closeSilently(statSource); JdbcUtils.closeSilently(statSource);
JdbcUtils.closeSilently(statTarget);
JdbcUtils.closeSilently(connSource); JdbcUtils.closeSilently(connSource);
JdbcUtils.closeSilently(connTarget);
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论