File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\tools\CreateCluster.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.tools;
8
9 import java.io.OutputStream;
10 import java.sql.Connection;
11 import java.sql.DriverManager;
12 import java.sql.SQLException;
13 import java.sql.Statement;
14 import org.h2.constant.ErrorCode;
15 import org.h2.engine.Constants;
16 import org.h2.store.fs.FileUtils;
17 import org.h2.util.IOUtils;
18 import org.h2.util.JdbcUtils;
19 import org.h2.util.Tool;
20
21 /**
22 * Creates a cluster from a standalone database.
23 * <br />
24 * Copies a database to another location if required.
25 * @h2.resource
26 */
27 public class CreateCluster extends Tool {
28
29 /**
30 * Options are case sensitive. Supported options are:
31 * <table>
32 * <tr><td>[-help] or [-?]</td>
33 * <td>Print the list of options</td></tr>
34 * <tr><td>[-urlSource "&lt;url&gt;"]</td>
35 * <td>The database URL of the source database (jdbc:h2:...)</td></tr>
36 * <tr><td>[-urlTarget "&lt;url&gt;"]</td>
37 * <td>The database URL of the target database (jdbc:h2:...)</td></tr>
38 * <tr><td>[-user &lt;user&gt;]</td>
39 * <td>The user name (default: sa)</td></tr>
40 * <tr><td>[-password &lt;pwd&gt;]</td>
41 * <td>The password</td></tr>
42 * <tr><td>[-serverList &lt;list&gt;]</td>
43 * <td>The comma separated list of host names or IP addresses</td></tr>
44 * </table>
45 * @h2.resource
46 *
47 * @param args the command line arguments
48 */
49 public static void main(String... args) throws SQLException {
50 new CreateCluster().runTool(args);
51 }
52
53 public void runTool(String... args) throws SQLException {
54 String urlSource = null;
55 String urlTarget = null;
56 String user = "sa";
57 String password = "";
58 String serverList = null;
59 for (int i = 0; args != null && i < args.length; i++) {
60 String arg = args[i];
61 if (arg.equals("-urlSource")) {
62 urlSource = args[++i];
63 } else if (arg.equals("-urlTarget")) {
64 urlTarget = args[++i];
65 } else if (arg.equals("-user")) {
66 user = args[++i];
67 } else if (arg.equals("-password")) {
68 password = args[++i];
69 } else if (arg.equals("-serverList")) {
70 serverList = args[++i];
71 } else if (arg.equals("-help") || arg.equals("-?")) {
72 showUsage();
73 return;
74 } else {
75 showUsageAndThrowUnsupportedOption(arg);
76 }
77 }
78 if (urlSource == null || urlTarget == null || serverList == null) {
79 showUsage();
80 throw new SQLException("Source URL, target URL, or server list not set");
81 }
82 process(urlSource, urlTarget, user, password, serverList);
83 }
84
85 /**
86 * Creates a cluster.
87 *
88 * @param urlSource the database URL of the original database
89 * @param urlTarget the database URL of the copy
90 * @param user the user name
91 * @param password the password
92 * @param serverList the server list
93 * @throws SQLException
94 */
95 public void execute(String urlSource, String urlTarget,
96 String user, String password, String serverList) throws SQLException {
97 process(urlSource, urlTarget, user, password, serverList);
98 }
99
100 private void process(String urlSource, String urlTarget,
101 String user, String password, String serverList) throws SQLException {
102 Connection connSource = null, connTarget = null;
103 Statement statSource = null, statTarget = null;
104 String scriptFile = "backup.sql";
105 try {
106 org.h2.Driver.load();
107
108 // verify that the database doesn't exist,
109 // or if it exists (an old cluster instance), it is deleted
110 boolean exists = true;
111 try {
112 connTarget = DriverManager.getConnection(urlTarget +
113 ";IFEXISTS=TRUE;CLUSTER=" + Constants.CLUSTERING_ENABLED,
114 user, password);
115 Statement stat = connTarget.createStatement();
116 stat.execute("DROP ALL OBJECTS DELETE FILES");
117 stat.close();
118 exists = false;
119 connTarget.close();
120 } catch (SQLException e) {
121 if (e.getErrorCode() == ErrorCode.DATABASE_NOT_FOUND_1) {
122 // database does not exists yet - ok
123 exists = false;
124 } else {
125 throw e;
126 }
127 }
128 if (exists) {
129 throw new SQLException("Target database must not yet exist. Please delete it first: " + urlTarget);
130 }
131
132 // use cluster='' so connecting is possible
133 // even if the cluster is enabled
134 connSource = DriverManager.getConnection(urlSource + ";CLUSTER=''", user, password);
135 statSource = connSource.createStatement();
136
137 // enable the exclusive mode and close other connections,
138 // so that data can't change while restoring the second database
139 statSource.execute("SET EXCLUSIVE 2");
140
141 try {
142
143 // backup
144 Script script = new Script();
145 script.setOut(out);
146 OutputStream scriptOut = null;
147 try {
148 scriptOut = FileUtils.newOutputStream(scriptFile, false);
149 Script.process(connSource, scriptOut);
150 } finally {
151 IOUtils.closeSilently(scriptOut);
152 }
153
154 // delete the target database and then restore
155 connTarget = DriverManager.getConnection(urlTarget + ";CLUSTER=''", user, password);
156 statTarget = connTarget.createStatement();
157 statTarget.execute("DROP ALL OBJECTS DELETE FILES");
158 connTarget.close();
159
160 RunScript runScript = new RunScript();
161 runScript.setOut(out);
162 runScript.process(urlTarget, user, password, scriptFile, null, false);
163
164 connTarget = DriverManager.getConnection(urlTarget, user, password);
165 statTarget = connTarget.createStatement();
166
167 // set the cluster to the serverList on both databases
168 statSource.executeUpdate("SET CLUSTER '" + serverList + "'");
169 statTarget.executeUpdate("SET CLUSTER '" + serverList + "'");
170 } finally {
171
172 // switch back to the regular mode
173 statSource.execute("SET EXCLUSIVE FALSE");
174 }
175 } finally {
176 FileUtils.delete(scriptFile);
177 JdbcUtils.closeSilently(statSource);
178 JdbcUtils.closeSilently(statTarget);
179 JdbcUtils.closeSilently(connSource);
180 JdbcUtils.closeSilently(connTarget);
181 }
182 }
183
184 }
185