KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > tools > CreateCluster


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.tools;
6
7 import java.io.File JavaDoc;
8 import java.sql.Connection JavaDoc;
9 import java.sql.DriverManager JavaDoc;
10 import java.sql.SQLException JavaDoc;
11 import java.sql.Statement JavaDoc;
12 import org.h2.message.Message;
13 import org.h2.util.JdbcUtils;
14
15 /**
16  * Tool to create a database cluster.
17  * This will copy a database to another location if required, and modify the cluster setting.
18  *
19  * @author Thomas
20  */

21 public class CreateCluster {
22     
23     private void showUsage() {
24         System.out.println("java "+getClass().getName()
25                 + " -urlSource <url> -urlTarget <url> -user <user> [-password <pwd>] -serverlist <serverlist>");
26     }
27
28     /**
29      * The command line interface for this tool.
30      * The options must be split into strings like this: "-urlSource", "jdbc:h2:test",...
31      * The following options are supported:
32      * <ul>
33      * <li>-help or -? (print the list of options)
34      * <li>-urlSource jdbc:h2:... (the database URL of the source database)
35      * <li>-urlTarget jdbc:h2:... (the database URL of the target database)
36      * </ul>
37      *
38      * @param args the command line arguments
39      * @throws SQLException
40      */

41     public static void main(String JavaDoc[] args) throws SQLException JavaDoc {
42         new CreateCluster().run(args);
43     }
44
45     private void run(String JavaDoc[] args) throws SQLException JavaDoc {
46         String JavaDoc urlSource = null;
47         String JavaDoc urlTarget = null;
48         String JavaDoc user = null;
49         String JavaDoc password = "";
50         String JavaDoc serverlist = null;
51         for(int i=0; args != null && i<args.length; i++) {
52             if(args[i].equals("-urlSource")) {
53                 urlSource = args[++i];
54             } else if(args[i].equals("-urlTarget")) {
55                 urlTarget = args[++i];
56             } else if(args[i].equals("-user")) {
57                 user = args[++i];
58             } else if(args[i].equals("-password")) {
59                 password = args[++i];
60             } else if(args[i].equals("-serverlist")) {
61                 serverlist = args[++i];
62             } else {
63                 showUsage();
64                 return;
65             }
66         }
67         if(urlSource==null || urlTarget==null || user==null || serverlist==null) {
68             showUsage();
69             return;
70         }
71         
72         execute(urlSource, urlTarget, user, password, serverlist);
73     }
74     
75     /**
76      * Creates a cluster.
77      *
78      * @param urlSource the database URL of the original database
79      * @param urlTarget the database URL of the copy
80      * @param user the user name
81      * @param password the password
82      * @param serverlist the server list
83      * @throws SQLException
84      */

85     public static void execute(String JavaDoc urlSource, String JavaDoc urlTarget, String JavaDoc user, String JavaDoc password, String JavaDoc serverlist) throws SQLException JavaDoc {
86         Connection JavaDoc conn = null;
87         Statement JavaDoc stat = null;
88         try {
89             org.h2.Driver.load();
90             
91             
92             // use cluster='' so connecting is possible even if the cluster is enabled
93
conn = DriverManager.getConnection(urlSource + ";CLUSTER=''", user, password);
94             conn.close();
95             try {
96                 conn = DriverManager.getConnection(urlTarget + ";IFEXISTS=TRUE", user, password);
97                 conn.close();
98                 throw new Exception JavaDoc("Target database must not yet exist. Please delete it first");
99             } catch(SQLException JavaDoc e) {
100                 // database does not exists - ok
101
}
102             
103             // TODO cluster: need to open the database in exclusive mode, so that other applications
104
// cannot change the data while it is restoring the second database. But there is currently no exclusive mode.
105

106             String JavaDoc scriptFile = "backup.sql";
107             Backup.execute(urlSource, user, password, scriptFile);
108             RunScript.execute(urlTarget, user, password, scriptFile, null, false);
109             new File JavaDoc(scriptFile).delete();
110             
111             // set the cluster to the serverlist on both databases
112
conn = DriverManager.getConnection(urlSource, user, password);
113             stat = conn.createStatement();
114             stat.executeUpdate("SET CLUSTER '" + serverlist + "'");
115             conn.close();
116             conn = DriverManager.getConnection(urlTarget, user, password);
117             stat = conn.createStatement();
118             stat.executeUpdate("SET CLUSTER '" + serverlist + "'");
119         } catch(Exception JavaDoc e) {
120             throw Message.convert(e);
121         } finally {
122             JdbcUtils.closeSilently(conn);
123             JdbcUtils.closeSilently(stat);
124         }
125     }
126     
127 }
128
Popular Tags