KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.store.LogChecksumSetup
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.sql.Connection JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 import javax.sql.DataSource JavaDoc;
31
32 import org.apache.derby.tools.ij;
33 import org.apache.derbyTesting.functionTests.util.TestUtil;
34
35 /*
36  * This test contains a recovery for a database that did recovery just
37  * before it went down. After recovery more records are inserted into
38  * the database before the database is shutdown. Then, roll-forward
39  * recovery of the database from the backup is performed. It is then
40  * checked that the records inserted after the first recovery is still
41  * present. This test was made to recreate the problem in DERBY-298.
42  * The test should be run after store/RecoveryAfterBackupSetup.java.
43  *
44  * @author oystein.grovlen@sun.com
45  * @see RecoveryAfterBackupSetup
46  */

47 public class RecoveryAfterBackup
48 {
49
50     public static void main(String JavaDoc[] argv) throws Throwable JavaDoc
51     {
52         try {
53             ij.getPropertyArg(argv);
54             Connection JavaDoc conn = ij.startJBMS();
55             conn.setAutoCommit(true);
56             
57             // After recovery table should contain two records with
58
// values 0 and 1
59
Statement JavaDoc s = conn.createStatement();
60             ResultSet JavaDoc rs = s.executeQuery("SELECT COUNT(a), SUM(a) FROM t1");
61             while (rs.next()) {
62                 int count = rs.getInt(1);
63                 int sum = rs.getInt(2);
64                 if (count!=2 || sum!=1) {
65                     System.out.print("Unexpected initial database state: ");
66                 }
67                 System.out.println("Count: " + count + " Sum: " + sum);
68             }
69
70             // Insert some more records
71
System.out.println("Inserting records ...");
72             s.execute ("INSERT INTO t1 SELECT a+2 FROM t1");
73             s.execute ("INSERT INTO t1 SELECT a+4 FROM t1");
74             s.execute ("INSERT INTO t1 SELECT a+8 FROM t1");
75             s.execute ("INSERT INTO t1 SELECT a+16 FROM t1");
76             s.execute ("INSERT INTO t1 SELECT a+32 FROM t1");
77             s.execute ("INSERT INTO t1 SELECT a+64 FROM t1");
78             s.execute ("INSERT INTO t1 SELECT a+128 FROM t1");
79
80             // Shut down database
81
System.out.println("Shutting down database ...");
82             try {
83                 TestUtil.getConnection("", "shutdown=true");
84             } catch(SQLException JavaDoc sqle) {
85                 if (sqle.getSQLState() != null
86                     && sqle.getSQLState().equals("XJ015")) {
87                     System.out.println("Database shutdown completed");
88                 } else {
89                     throw sqle;
90                 }
91             }
92
93             // Start up with rollforward-recovery
94
System.out.println("Starting restore with roll-forward recovery..");
95             String JavaDoc dbName = "hairynosedwombat";
96             String JavaDoc connAttrs =
97                 "rollForwardRecoveryFrom=extinout/mybackup/hairynosedwombat";
98             conn = TestUtil.getConnection(dbName, connAttrs);
99
100             // After restore table should contain all records inserted above
101
System.out.println("Verifying database ...");
102             s = conn.createStatement();
103             rs = s.executeQuery("SELECT COUNT(a), SUM(a) FROM t1");
104             while (rs.next()) {
105                 int count = rs.getInt(1);
106                 int sum = rs.getInt(2);
107                 if (count!=256 || sum!=256*255/2) { // sum 0..n = n*(n-1)/2
108
System.out.print("Test FAILED: ");
109                 }
110                 System.out.println("Count: " + count + " Sum: " + sum);
111             }
112
113         } catch (SQLException JavaDoc sqle) {
114             org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out,
115                                                                     sqle);
116             sqle.printStackTrace(System.out);
117         }
118     }
119 }
120
Popular Tags