001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.tools;
007:
008: import java.sql.Connection;
009: import java.sql.DriverManager;
010: import java.sql.SQLException;
011: import java.sql.Statement;
012:
013: import org.h2.util.FileUtils;
014: import org.h2.util.JdbcUtils;
015:
016: /**
017: * Tool to create a database cluster. This will copy a database to another
018: * location if required, and modify the cluster setting.
019: */
020: public class CreateCluster {
021:
022: private void showUsage() {
023: System.out
024: .println("java "
025: + getClass().getName()
026: + " -urlSource <url> -urlTarget <url> -user <user> [-password <pwd>] -serverlist <serverlist>");
027: System.out
028: .println("See also http://h2database.com/javadoc/org/h2/tools/CreateCluster.html");
029: }
030:
031: /**
032: * The command line interface for this tool. The options must be split into
033: * strings like this: "-urlSource", "jdbc:h2:test",... Options are case
034: * sensitive. The following options are supported:
035: * <ul>
036: * <li>-help or -? (print the list of options) </li>
037: * <li>-urlSource jdbc:h2:... (the database URL of the source database)
038: * </li>
039: * <li>-urlTarget jdbc:h2:... (the database URL of the target database)
040: * </li>
041: * </ul>
042: *
043: * @param args the command line arguments
044: * @throws SQLException
045: */
046: public static void main(String[] args) throws SQLException {
047: new CreateCluster().run(args);
048: }
049:
050: private void run(String[] args) throws SQLException {
051: String urlSource = null;
052: String urlTarget = null;
053: String user = null;
054: String password = "";
055: String serverlist = null;
056: for (int i = 0; args != null && i < args.length; i++) {
057: if (args[i].equals("-urlSource")) {
058: urlSource = args[++i];
059: } else if (args[i].equals("-urlTarget")) {
060: urlTarget = args[++i];
061: } else if (args[i].equals("-user")) {
062: user = args[++i];
063: } else if (args[i].equals("-password")) {
064: password = args[++i];
065: } else if (args[i].equals("-serverlist")) {
066: serverlist = args[++i];
067: } else {
068: showUsage();
069: return;
070: }
071: }
072: if (urlSource == null || urlTarget == null || user == null
073: || serverlist == null) {
074: showUsage();
075: return;
076: }
077:
078: execute(urlSource, urlTarget, user, password, serverlist);
079: }
080:
081: /**
082: * Creates a cluster.
083: *
084: * @param urlSource the database URL of the original database
085: * @param urlTarget the database URL of the copy
086: * @param user the user name
087: * @param password the password
088: * @param serverlist the server list
089: * @throws SQLException
090: */
091: public static void execute(String urlSource, String urlTarget,
092: String user, String password, String serverlist)
093: throws SQLException {
094: Connection conn = null;
095: Statement stat = null;
096: try {
097: org.h2.Driver.load();
098: // use cluster='' so connecting is possible even if the cluster is enabled
099: conn = DriverManager.getConnection(urlSource
100: + ";CLUSTER=''", user, password);
101: conn.close();
102: boolean exists;
103: try {
104: conn = DriverManager.getConnection(urlTarget
105: + ";IFEXISTS=TRUE", user, password);
106: conn.close();
107: exists = true;
108: } catch (SQLException e) {
109: // database does not exists - ok
110: exists = false;
111: }
112: if (exists) {
113: throw new SQLException(
114: "Target database must not yet exist. Please delete it first");
115: }
116:
117: // TODO cluster: need to open the database in exclusive mode,
118: // so that other applications
119: // cannot change the data while it is restoring the second database.
120: // But there is currently no exclusive mode.
121:
122: String scriptFile = "backup.sql";
123: Script.execute(urlSource, user, password, scriptFile);
124: RunScript.execute(urlTarget, user, password, scriptFile,
125: null, false);
126: FileUtils.delete(scriptFile);
127:
128: // set the cluster to the serverlist on both databases
129: conn = DriverManager.getConnection(urlSource, user,
130: password);
131: stat = conn.createStatement();
132: stat.executeUpdate("SET CLUSTER '" + serverlist + "'");
133: conn.close();
134: conn = DriverManager.getConnection(urlTarget, user,
135: password);
136: stat = conn.createStatement();
137: stat.executeUpdate("SET CLUSTER '" + serverlist + "'");
138: } finally {
139: JdbcUtils.closeSilently(conn);
140: JdbcUtils.closeSilently(stat);
141: }
142: }
143:
144: }
|