001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.store.backupRestore1
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.io.File;
025: import java.io.ByteArrayInputStream;
026: import java.io.RandomAccessFile;
027: import java.math.BigDecimal;
028:
029: import java.sql.CallableStatement;
030: import java.sql.Connection;
031: import java.sql.SQLException;
032: import java.sql.PreparedStatement;
033: import java.sql.ResultSet;
034: import java.sql.Statement;
035: import org.apache.derby.tools.ij;
036: import org.apache.derby.tools.JDBCDisplayUtil;
037: import org.apache.derbyTesting.functionTests.util.TestUtil;
038:
039: /**
040: * Test of backup restore through java program JDBC calls.
041: * Enhanced the test from bug5229 repro.
042: * @author suresht
043: */
044:
045: public class backupRestore1 {
046: private static final byte[] blob1 = { 1, 2, 3, 4, 5, 6, 7, 8 };
047: private static final byte[] blob2 = new byte[0x4001];
048: private static final byte[] blob3 = new byte[0x8000];
049: private static final byte[] blob4 = new byte[32700];
050: private static final byte[] clob1 = { 'a', 'b', 'c', 'd', 'e', 'f',
051: 'g', 'h' };
052: private static final byte[] clob2 = new byte[0x4001];
053: private static final byte[] clob3 = new byte[0x8000];
054: private static final byte[] clob4 = new byte[0x1000];
055: static {
056: for (int i = 0; i < clob2.length; i++)
057: clob2[i] = 'a';
058: for (int i = 0; i < clob3.length; i++)
059: clob3[i] = 'b';
060: for (int i = 0; i < clob4.length; i++)
061: clob4[i] = 'c';
062: }
063:
064: public static void main(String args[]) {
065:
066: System.out.println("Test backupRestore starting");
067: try {
068: // use the ij utility to read the property file and
069: // make the initial connection.
070: ij.getPropertyArg(args);
071: Connection conn = ij.startJBMS();
072: Statement stmt = conn.createStatement();
073: stmt
074: .execute("CREATE FUNCTION ConsistencyChecker() RETURNS VARCHAR(128) EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.T_ConsistencyChecker.runConsistencyChecker' LANGUAGE JAVA PARAMETER STYLE JAVA");
075:
076: stmt
077: .executeUpdate("create table t( id integer not null primary key, cBlob blob(64K),"
078: + "cClob clob(64K), clvarchar long varchar, clvarbinary long varchar for bit data)");
079: conn.setAutoCommit(false);
080: PreparedStatement insStmt = conn
081: .prepareStatement("insert into t( id, cBlob, cClob, clvarchar, clvarbinary) values( ?, ?, ?, ?, ?)");
082: insStmt.setInt(1, 1);
083: insStmt.setBinaryStream(2, new ByteArrayInputStream(blob1),
084: blob1.length);
085: insStmt.setAsciiStream(3, new ByteArrayInputStream(clob1),
086: clob1.length);
087: insStmt.setAsciiStream(4, new ByteArrayInputStream(clob2),
088: clob2.length);
089: insStmt.setBinaryStream(5, new ByteArrayInputStream(blob2),
090: blob2.length);
091: insStmt.executeUpdate();
092: insStmt.setInt(1, 2);
093: insStmt.setBinaryStream(2, new ByteArrayInputStream(blob3),
094: blob3.length);
095: insStmt.setAsciiStream(3, new ByteArrayInputStream(clob3),
096: clob3.length);
097: insStmt.setAsciiStream(4, new ByteArrayInputStream(clob4),
098: clob4.length);
099: insStmt.setBinaryStream(5, new ByteArrayInputStream(blob4),
100: blob4.length);
101: insStmt.executeUpdate();
102: conn.commit();
103:
104: //execute the backup command.
105: CallableStatement backupStmt = conn
106: .prepareCall("CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(?, ?)");
107: backupStmt.setString(1, "extinout/mybackup");
108: backupStmt.setInt(2, 1);
109: backupStmt.execute();
110: backupStmt.close();
111:
112: //insert a row after the bacup
113: insStmt.setInt(1, 3);
114: insStmt.setBinaryStream(2, new ByteArrayInputStream(blob3),
115: blob3.length);
116: insStmt.setAsciiStream(3, new ByteArrayInputStream(clob3),
117: clob3.length);
118: insStmt.setAsciiStream(4, new ByteArrayInputStream(clob4),
119: clob4.length);
120: insStmt.setBinaryStream(5, new ByteArrayInputStream(blob4),
121: blob4.length);
122: insStmt.executeUpdate();
123: conn.commit();
124: insStmt.close();
125: conn.close();
126: } catch (SQLException e) {
127: dumpSQLExceptions(e);
128: } catch (Throwable e) {
129: System.out.println("FAIL -- unexpected exception:"
130: + e.toString());
131: }
132:
133: //shutdown the database ..
134: try {
135: //shutdown
136: TestUtil.getConnection("wombat", "shutdown=true");
137: } catch (SQLException se) {
138: if (se.getSQLState() != null
139: && se.getSQLState().equals("08006"))
140: System.out.println("database shutdown properly");
141: else
142: dumpSQLExceptions(se);
143: } catch (Throwable e) {
144: System.out.println("FAIL -- unexpected exception:"
145: + e.toString());
146: }
147:
148: System.out.println("testing rollforward recovery");
149: try {
150: //perform rollforward recovery and do some inserts again
151: Connection conn = TestUtil.getConnection("wombat",
152: "rollForwardRecoveryFrom=extinout/mybackup/wombat");
153: //run consistenct checker
154: Statement stmt = conn.createStatement();
155: stmt.execute("VALUES (ConsistencyChecker())");
156:
157: //make sure the db has three rows
158: ResultSet rs = stmt.executeQuery("select count(*) from t");
159: while (rs.next()) {
160: int count = rs.getInt(1);
161: System.out.println(count);
162: }
163:
164: conn.commit();
165: conn.close();
166: TestUtil.getConnection("wombat", "shutdown=true");
167: } catch (SQLException se) {
168: if (se.getSQLState() != null
169: && se.getSQLState().equals("08006"))
170: System.out.println("database shutdown properly");
171: else
172: dumpSQLExceptions(se);
173: } catch (Throwable e) {
174: System.out.println("FAIL -- unexpected exception:"
175: + e.toString());
176: }
177:
178: //make sure that good back does not get deleted if renaming existing
179: //backup as old backup fails. (beetle : 5336)
180: RandomAccessFile rfs = null;
181: boolean alreadyShutdown = false;
182: try {
183: Connection conn = TestUtil.getConnection("wombat", null);
184:
185: //just open to a file in existing backup, so that rename will fail on
186: //next backup
187: rfs = new RandomAccessFile(
188: "extinout/mybackup/wombat/service.properties", "r");
189:
190: CallableStatement backupStmt = conn
191: .prepareCall("CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE(?)");
192: backupStmt.setString(1, "extinout/mybackup");
193: backupStmt.execute();
194: backupStmt.close();
195: conn.close();
196: TestUtil.getConnection("wombat", "shutdown=true");
197: } catch (SQLException se) {
198: if (se.getSQLState() != null
199: && se.getSQLState().equals("XSRS4")) {
200: alreadyShutdown = false;
201: //expected exception:XSRS4;rename failed because of a open file
202: } else if (se.getSQLState() != null
203: && se.getSQLState().equals("08006")) { //On UNIX Systems , rename does not fail even if there is a
204: //open file, if we succefully reached shutdown mean
205: //everything is okay.
206: System.out.println("database shutdown properly");
207: alreadyShutdown = true;
208: } else
209: dumpSQLExceptions(se);
210: } catch (Throwable e) {
211: System.out.println("FAIL -- unexpected exception:"
212: + e.toString());
213: }
214:
215: //shutdown the db
216: if (!alreadyShutdown) {
217: try {
218: //shutdown
219: TestUtil.getConnection("wombat", "shutdown=true");
220: } catch (SQLException se) {
221: if (se.getSQLState() != null
222: && se.getSQLState().equals("08006"))
223: System.out.println("database shutdown properly");
224: else
225: dumpSQLExceptions(se);
226: } catch (Throwable e) {
227: System.out.println("FAIL -- unexpected exception:"
228: + e.toString());
229: }
230: }
231:
232: //restore from the backup db and run consistency checker on it.
233: try {
234: //close the earlier opened file in backup dir
235: if (rfs != null)
236: rfs.close();
237:
238: Connection conn = TestUtil.getConnection("wombat",
239: "restoreFrom=extinout/mybackup/wombat");
240:
241: //run consistenct checker
242: Statement stmt = conn.createStatement();
243: stmt.execute("VALUES (ConsistencyChecker())");
244: conn.close();
245: //shutdown the backup db;
246: TestUtil.getConnection("wombat", "shutdown=true");
247: } catch (SQLException se) {
248: if (se.getSQLState() != null
249: && se.getSQLState().equals("08006"))
250: System.out.println("database shutdown properly");
251: else
252: dumpSQLExceptions(se);
253: } catch (Throwable e) {
254: System.out.println("FAIL -- unexpected exception:"
255: + e.toString());
256: }
257:
258: //now take a backup again , just to make all is well in the system.
259: try {
260: Connection conn = TestUtil.getConnection("wombat", null);
261:
262: CallableStatement backupStmt = conn
263: .prepareCall("CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE(?)");
264: backupStmt.setString(1, "extinout/mybackup");
265: backupStmt.execute();
266: backupStmt.close();
267:
268: Statement stmt = conn.createStatement();
269: stmt.execute("VALUES (ConsistencyChecker())");
270: conn.close();
271: TestUtil.getConnection("wombat", "shutdown=true");
272: } catch (SQLException se) {
273: if (se.getSQLState() != null
274: && se.getSQLState().equals("08006"))
275: System.out.println("database shutdown properly");
276: else
277: dumpSQLExceptions(se);
278: } catch (Throwable e) {
279: System.out.println("FAIL -- unexpected exception:"
280: + e.toString());
281: }
282:
283: System.out.println("Test backupRestore1 finished");
284: }
285:
286: static private void dumpSQLExceptions(SQLException se) {
287: System.out.println("FAIL -- unexpected exception: "
288: + se.toString());
289: SQLException lastSe = se;
290: while (se != null) {
291: System.out.print("SQLSTATE(" + se.getSQLState() + "):");
292: lastSe = se;
293: se = se.getNextException();
294: }
295: System.out.println("");
296: lastSe.printStackTrace(System.out);
297: }
298:
299: }
|