001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.store.LogChecksumSetup
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.store;
023:
024: import java.sql.Connection;
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027: import java.sql.Statement;
028: import java.util.Properties;
029:
030: import javax.sql.DataSource;
031:
032: import org.apache.derby.tools.ij;
033: import org.apache.derbyTesting.functionTests.util.TestUtil;
034:
035: /*
036: * This test contains a recovery for a database that did recovery just
037: * before it went down. After recovery more records are inserted into
038: * the database before the database is shutdown. Then, roll-forward
039: * recovery of the database from the backup is performed. It is then
040: * checked that the records inserted after the first recovery is still
041: * present. This test was made to recreate the problem in DERBY-298.
042: * The test should be run after store/RecoveryAfterBackupSetup.java.
043: *
044: * @author oystein.grovlen@sun.com
045: * @see RecoveryAfterBackupSetup
046: */
047: public class RecoveryAfterBackup {
048:
049: public static void main(String[] argv) throws Throwable {
050: try {
051: ij.getPropertyArg(argv);
052: Connection conn = ij.startJBMS();
053: conn.setAutoCommit(true);
054:
055: // After recovery table should contain two records with
056: // values 0 and 1
057: Statement s = conn.createStatement();
058: ResultSet rs = s
059: .executeQuery("SELECT COUNT(a), SUM(a) FROM t1");
060: while (rs.next()) {
061: int count = rs.getInt(1);
062: int sum = rs.getInt(2);
063: if (count != 2 || sum != 1) {
064: System.out
065: .print("Unexpected initial database state: ");
066: }
067: System.out.println("Count: " + count + " Sum: " + sum);
068: }
069:
070: // Insert some more records
071: System.out.println("Inserting records ...");
072: s.execute("INSERT INTO t1 SELECT a+2 FROM t1");
073: s.execute("INSERT INTO t1 SELECT a+4 FROM t1");
074: s.execute("INSERT INTO t1 SELECT a+8 FROM t1");
075: s.execute("INSERT INTO t1 SELECT a+16 FROM t1");
076: s.execute("INSERT INTO t1 SELECT a+32 FROM t1");
077: s.execute("INSERT INTO t1 SELECT a+64 FROM t1");
078: s.execute("INSERT INTO t1 SELECT a+128 FROM t1");
079:
080: // Shut down database
081: System.out.println("Shutting down database ...");
082: try {
083: TestUtil.getConnection("", "shutdown=true");
084: } catch (SQLException sqle) {
085: if (sqle.getSQLState() != null
086: && sqle.getSQLState().equals("XJ015")) {
087: System.out.println("Database shutdown completed");
088: } else {
089: throw sqle;
090: }
091: }
092:
093: // Start up with rollforward-recovery
094: System.out
095: .println("Starting restore with roll-forward recovery..");
096: String dbName = "hairynosedwombat";
097: String connAttrs = "rollForwardRecoveryFrom=extinout/mybackup/hairynosedwombat";
098: conn = TestUtil.getConnection(dbName, connAttrs);
099:
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 sqle) {
114: org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(
115: System.out, sqle);
116: sqle.printStackTrace(System.out);
117: }
118: }
119: }
|