KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.store.backupRestore1
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
24 import java.io.File JavaDoc;
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.RandomAccessFile JavaDoc;
27 import java.math.BigDecimal JavaDoc;
28
29 import java.sql.CallableStatement JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.sql.PreparedStatement JavaDoc;
33 import java.sql.ResultSet JavaDoc;
34 import java.sql.Statement JavaDoc;
35 import org.apache.derby.tools.ij;
36 import org.apache.derby.tools.JDBCDisplayUtil;
37 import org.apache.derbyTesting.functionTests.util.TestUtil;
38
39 /**
40  * Test of backup restore through java program JDBC calls.
41  * Enhanced the test from bug5229 repro.
42  * @author suresht
43  */

44
45 public class backupRestore1
46 {
47     private static final byte[] blob1 = { 1, 2, 3, 4, 5, 6, 7, 8};
48     private static final byte[] blob2 = new byte[0x4001];
49     private static final byte[] blob3 = new byte[0x8000];
50     private static final byte[] blob4 = new byte[32700];
51     private static final byte[] clob1 = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
52     private static final byte[] clob2 = new byte[0x4001];
53     private static final byte[] clob3 = new byte[0x8000];
54     private static final byte[] clob4 = new byte[0x1000];
55     static
56     {
57         for( int i = 0; i < clob2.length; i++)
58             clob2[i] = 'a';
59         for( int i = 0; i < clob3.length; i++)
60             clob3[i] = 'b';
61         for( int i = 0; i < clob4.length; i++)
62             clob4[i] = 'c';
63     }
64
65     public static void main( String JavaDoc args[])
66     {
67
68         System.out.println("Test backupRestore starting");
69         try
70         {
71             // use the ij utility to read the property file and
72
// make the initial connection.
73
ij.getPropertyArg(args);
74             Connection JavaDoc conn = ij.startJBMS();
75             Statement JavaDoc stmt = conn.createStatement();
76             stmt.execute("CREATE FUNCTION ConsistencyChecker() RETURNS VARCHAR(128) EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.T_ConsistencyChecker.runConsistencyChecker' LANGUAGE JAVA PARAMETER STYLE JAVA");
77
78             stmt.executeUpdate( "create table t( id integer not null primary key, cBlob blob(64K),"
79                 + "cClob clob(64K), clvarchar long varchar, clvarbinary long varchar for bit data)");
80             conn.setAutoCommit( false);
81             PreparedStatement JavaDoc insStmt = conn.prepareStatement( "insert into t( id, cBlob, cClob, clvarchar, clvarbinary) values( ?, ?, ?, ?, ?)");
82             insStmt.setInt( 1, 1);
83             insStmt.setBinaryStream( 2, new ByteArrayInputStream JavaDoc( blob1), blob1.length);
84             insStmt.setAsciiStream( 3, new ByteArrayInputStream JavaDoc( clob1), clob1.length);
85             insStmt.setAsciiStream( 4, new ByteArrayInputStream JavaDoc( clob2), clob2.length);
86             insStmt.setBinaryStream( 5, new ByteArrayInputStream JavaDoc( blob2), blob2.length);
87             insStmt.executeUpdate();
88             insStmt.setInt( 1, 2);
89             insStmt.setBinaryStream( 2, new ByteArrayInputStream JavaDoc(blob3), blob3.length);
90             insStmt.setAsciiStream( 3, new ByteArrayInputStream JavaDoc( clob3), clob3.length);
91             insStmt.setAsciiStream( 4, new ByteArrayInputStream JavaDoc( clob4), clob4.length);
92             insStmt.setBinaryStream( 5, new ByteArrayInputStream JavaDoc( blob4), blob4.length);
93             insStmt.executeUpdate();
94             conn.commit();
95
96             //execute the backup command.
97
CallableStatement JavaDoc backupStmt = conn.prepareCall(
98                 "CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(?, ?)");
99             backupStmt.setString(1, "extinout/mybackup");
100             backupStmt.setInt(2, 1);
101             backupStmt.execute();
102             backupStmt.close();
103
104             //insert a row after the bacup
105
insStmt.setInt( 1, 3);
106             insStmt.setBinaryStream( 2, new ByteArrayInputStream JavaDoc(blob3), blob3.length);
107             insStmt.setAsciiStream( 3, new ByteArrayInputStream JavaDoc( clob3), clob3.length);
108             insStmt.setAsciiStream( 4, new ByteArrayInputStream JavaDoc( clob4), clob4.length);
109             insStmt.setBinaryStream( 5, new ByteArrayInputStream JavaDoc( blob4), blob4.length);
110             insStmt.executeUpdate();
111             conn.commit();
112             insStmt.close();
113             conn.close();
114         }
115         catch( SQLException JavaDoc e)
116         {
117             dumpSQLExceptions(e);
118         } catch (Throwable JavaDoc e) {
119             System.out.println("FAIL -- unexpected exception:" + e.toString());
120         }
121
122         //shutdown the database ..
123
try{
124             //shutdown
125
TestUtil.getConnection("wombat", "shutdown=true");
126         }catch(SQLException JavaDoc se){
127                 if (se.getSQLState() != null && se.getSQLState().equals("08006"))
128                     System.out.println("database shutdown properly");
129                 else
130                     dumpSQLExceptions(se);
131         } catch (Throwable JavaDoc e) {
132             System.out.println("FAIL -- unexpected exception:" + e.toString());
133         }
134
135         System.out.println("testing rollforward recovery");
136         try{
137             //perform rollforward recovery and do some inserts again
138
Connection JavaDoc conn = TestUtil.getConnection("wombat", "rollForwardRecoveryFrom=extinout/mybackup/wombat");
139             //run consistenct checker
140
Statement JavaDoc stmt = conn.createStatement();
141             stmt.execute("VALUES (ConsistencyChecker())");
142
143             //make sure the db has three rows
144
ResultSet JavaDoc rs = stmt.executeQuery("select count(*) from t");
145             while (rs.next()) {
146                 int count = rs.getInt(1);
147                 System.out.println(count);
148             }
149
150             conn.commit();
151             conn.close();
152             TestUtil.getConnection("wombat", "shutdown=true");
153         }
154         catch( SQLException JavaDoc se)
155         {
156             if (se.getSQLState() != null && se.getSQLState().equals("08006"))
157                 System.out.println("database shutdown properly");
158             else
159                 dumpSQLExceptions(se);
160         } catch (Throwable JavaDoc e) {
161             System.out.println("FAIL -- unexpected exception:" + e.toString());
162         }
163
164         
165         //make sure that good back does not get deleted if renaming existing
166
//backup as old backup fails. (beetle : 5336)
167
RandomAccessFile JavaDoc rfs = null;
168         boolean alreadyShutdown = false;
169         try{
170             Connection JavaDoc conn = TestUtil.getConnection("wombat", null);
171                                 
172             //just open to a file in existing backup, so that rename will fail on
173
//next backup
174
rfs =
175                 new RandomAccessFile JavaDoc(
176                     "extinout/mybackup/wombat/service.properties" , "r");
177
178             CallableStatement JavaDoc backupStmt = conn.prepareCall(
179                 "CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE(?)");
180             backupStmt.setString(1, "extinout/mybackup");
181             backupStmt.execute();
182             backupStmt.close();
183             conn.close();
184             TestUtil.getConnection("wombat", "shutdown=true");
185         }catch(SQLException JavaDoc se)
186         {
187             if (se.getSQLState() != null && se.getSQLState().equals("XSRS4"))
188             { alreadyShutdown = false;
189                 //expected exception:XSRS4;rename failed because of a open file
190
}else
191                 if (se.getSQLState() != null &&
192                     se.getSQLState().equals("08006"))
193                 { //On UNIX Systems , rename does not fail even if there is a
194
//open file, if we succefully reached shutdown mean
195
//everything is okay.
196
System.out.println("database shutdown properly");
197                     alreadyShutdown = true;
198                 }else
199                     dumpSQLExceptions(se);
200         }catch (Throwable JavaDoc e) {
201             System.out.println("FAIL -- unexpected exception:" + e.toString());
202         }
203
204         //shutdown the db
205
if(!alreadyShutdown)
206         {
207             try{
208                 //shutdown
209
TestUtil.getConnection("wombat", "shutdown=true");
210             }catch(SQLException JavaDoc se){
211                 if (se.getSQLState() != null && se.getSQLState().equals("08006"))
212                     System.out.println("database shutdown properly");
213                 else
214                     dumpSQLExceptions(se);
215             } catch (Throwable JavaDoc e) {
216                 System.out.println("FAIL -- unexpected exception:" + e.toString());
217             }
218         }
219
220         //restore from the backup db and run consistency checker on it.
221
try{
222             //close the earlier opened file in backup dir
223
if(rfs != null )
224                 rfs.close();
225
226             Connection JavaDoc conn = TestUtil.getConnection("wombat", "restoreFrom=extinout/mybackup/wombat");
227             
228             //run consistenct checker
229
Statement JavaDoc stmt = conn.createStatement();
230             stmt.execute("VALUES (ConsistencyChecker())");
231             conn.close();
232             //shutdown the backup db;
233
TestUtil.getConnection("wombat", "shutdown=true");
234         }catch(SQLException JavaDoc se)
235         {
236             if (se.getSQLState() != null && se.getSQLState().equals("08006"))
237                 System.out.println("database shutdown properly");
238             else
239                 dumpSQLExceptions(se);
240         }catch (Throwable JavaDoc e) {
241             System.out.println("FAIL -- unexpected exception:" + e.toString());
242         }
243
244         //now take a backup again , just to make all is well in the system.
245
try{
246             Connection JavaDoc conn = TestUtil.getConnection("wombat", null);
247             
248             CallableStatement JavaDoc backupStmt = conn.prepareCall(
249                 "CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE(?)");
250             backupStmt.setString(1, "extinout/mybackup");
251             backupStmt.execute();
252             backupStmt.close();
253
254             Statement JavaDoc stmt = conn.createStatement();
255             stmt.execute("VALUES (ConsistencyChecker())");
256             conn.close();
257             TestUtil.getConnection("wombat", "shutdown=true");
258         }catch(SQLException JavaDoc se)
259         {
260             if (se.getSQLState() != null && se.getSQLState().equals("08006"))
261                 System.out.println("database shutdown properly");
262             else
263                 dumpSQLExceptions(se);
264         }catch (Throwable JavaDoc e) {
265             System.out.println("FAIL -- unexpected exception:" + e.toString());
266         }
267
268
269         System.out.println("Test backupRestore1 finished");
270     }
271
272     
273     static private void dumpSQLExceptions (SQLException JavaDoc se) {
274         System.out.println("FAIL -- unexpected exception: " + se.toString());
275         SQLException JavaDoc lastSe = se;
276         while (se != null) {
277             System.out.print("SQLSTATE("+se.getSQLState()+"):");
278             lastSe = se;
279             se = se.getNextException();
280         }
281         System.out.println("");
282         lastSe.printStackTrace(System.out);
283     }
284     
285 }
286
287
288
Popular Tags