KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > store > OnlineBackup


1 /*
2
3 Derby - Class org.apache.derbyTesting.functionTests.store.OnlineBackup
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.store;
23 import java.sql.Connection JavaDoc;
24 import java.sql.CallableStatement JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import org.apache.derbyTesting.functionTests.util.TestUtil;
27
28 /**
29  * This class provides functionalty for tests to perform
30  * online backup in a separate thread. And functions to
31  * create/restore/rollforard recovery from the backup.
32  *
33  * @author <a HREF="mailto:suresh.thalamati@gmail.com">Suresh Thalamati</a>
34  * @version 1.0
35  */

36
37 public class OnlineBackup implements Runnable JavaDoc{
38
39     private String JavaDoc dbName; // name of the database to backup
40
private boolean beginBackup = false;
41     private boolean endBackup = false;
42     private boolean backupFailed = false;
43     private Throwable JavaDoc backupError = null;
44     private String JavaDoc backupPath;
45
46     OnlineBackup(String JavaDoc dbName, String JavaDoc backupPath) {
47         this.dbName = dbName;
48         this.backupPath = backupPath;
49     }
50
51     /**
52      * implementation of run() method in the Runnable interface, which
53      * is invoked when a thread is started using this class object.
54      *
55      * Performs online backup.
56      *
57      */

58     public void run() {
59         backupFailed = false;
60         try {
61             performBackup();
62         } catch (Throwable JavaDoc error) {
63             synchronized(this) {
64                 // inform threads that may be waiting for backup to
65
// start/end that it failed.
66
backupFailed = true;
67                 backupError = error;
68                 notifyAll();
69             }
70             org.apache.derby.tools.JDBCDisplayUtil.ShowException(System.out, error);
71             error.printStackTrace(System.out);
72         }
73     }
74
75     /**
76      * Backup the database
77      */

78     void performBackup() throws SQLException JavaDoc {
79         Connection JavaDoc conn = TestUtil.getConnection(dbName , "");
80         CallableStatement JavaDoc backupStmt =
81             conn.prepareCall("CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE(?)");
82         backupStmt.setString(1, backupPath);
83             
84         synchronized(this) {
85             beginBackup = true;
86             endBackup = false;
87             notifyAll();
88         }
89
90         backupStmt.execute();
91         backupStmt.close();
92         conn.close();
93
94         synchronized(this) {
95             beginBackup = false;
96             endBackup = true;
97             notifyAll();
98         }
99     }
100
101     /**
102      * Wait for the backup to start.
103      */

104
105     public void waitForBackupToBegin() throws Exception JavaDoc{
106         synchronized(this) {
107             //wait for backup to begin
108
while (!beginBackup) {
109                 // if the backup failed for some reason throw error, don't go
110
// into wait state.
111
if (backupFailed)
112                     throw new Exception JavaDoc("BACKUP FAILED:" +
113                                         backupError.getMessage());
114                 else
115                     wait();
116             }
117         }
118     }
119     
120     /*
121      * Wait for the backup to finish.
122      */

123     public void waitForBackupToEnd() throws Exception JavaDoc{
124         synchronized(this) {
125             if (!endBackup) {
126                 // check if a backup has actually started by the test
127
if (!beginBackup) {
128                     System.out.println("BACKUP IS NOT STARTED BY THE TEST YET");
129                 } else {
130
131                     //wait for backup to finish
132
while (!endBackup)
133                     {
134                         // if the backup failed for some reason throw error, don't go
135
// into wait state.
136
if (backupFailed)
137                             throw new Exception JavaDoc("BACKUP FAILED:" +
138                                                 backupError.getMessage());
139                         else
140                             wait();
141                     }
142                 }
143             }
144
145         }
146     }
147
148     /**
149      * Check if backup is running ?
150      * @return <tt>true</tt> if backup is running.
151      * <tt>false</tt> otherwise.
152      */

153     public synchronized boolean isRunning() {
154         return beginBackup;
155     }
156     
157     /**
158      * Create a new database from the backup copy taken earlier.
159      * @param newDbName name of the database to be created.
160      */

161     public void createFromBackup(String JavaDoc newDbName) throws SQLException JavaDoc {
162         
163         Connection JavaDoc conn = TestUtil.getConnection(newDbName,
164                                         "createFrom=" +
165                                         backupPath + "/" +
166                                         dbName);
167         conn.close();
168         
169     }
170
171     
172     /**
173      * Restore the database from the backup copy taken earlier.
174      */

175     public void restoreFromBackup() throws SQLException JavaDoc {
176        
177         Connection JavaDoc conn = TestUtil.getConnection(dbName,
178                                         "restoreFrom=" +
179                                         backupPath + "/" +
180                                         dbName);
181
182         conn.close();
183     }
184 }
185
Popular Tags