1 21 22 package org.apache.derbyTesting.functionTests.tests.store; 23 24 import java.io.File ; 25 import java.io.ByteArrayInputStream ; 26 import java.io.RandomAccessFile ; 27 import java.math.BigDecimal ; 28 29 import java.sql.CallableStatement ; 30 import java.sql.Connection ; 31 import java.sql.SQLException ; 32 import java.sql.PreparedStatement ; 33 import java.sql.ResultSet ; 34 import java.sql.Statement ; 35 import org.apache.derby.tools.ij; 36 import org.apache.derby.tools.JDBCDisplayUtil; 37 import org.apache.derbyTesting.functionTests.util.TestUtil; 38 39 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 args[]) 66 { 67 68 System.out.println("Test backupRestore starting"); 69 try 70 { 71 ij.getPropertyArg(args); 74 Connection conn = ij.startJBMS(); 75 Statement 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 insStmt = conn.prepareStatement( "insert into t( id, cBlob, cClob, clvarchar, clvarbinary) values( ?, ?, ?, ?, ?)"); 82 insStmt.setInt( 1, 1); 83 insStmt.setBinaryStream( 2, new ByteArrayInputStream ( blob1), blob1.length); 84 insStmt.setAsciiStream( 3, new ByteArrayInputStream ( clob1), clob1.length); 85 insStmt.setAsciiStream( 4, new ByteArrayInputStream ( clob2), clob2.length); 86 insStmt.setBinaryStream( 5, new ByteArrayInputStream ( blob2), blob2.length); 87 insStmt.executeUpdate(); 88 insStmt.setInt( 1, 2); 89 insStmt.setBinaryStream( 2, new ByteArrayInputStream (blob3), blob3.length); 90 insStmt.setAsciiStream( 3, new ByteArrayInputStream ( clob3), clob3.length); 91 insStmt.setAsciiStream( 4, new ByteArrayInputStream ( clob4), clob4.length); 92 insStmt.setBinaryStream( 5, new ByteArrayInputStream ( blob4), blob4.length); 93 insStmt.executeUpdate(); 94 conn.commit(); 95 96 CallableStatement 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 insStmt.setInt( 1, 3); 106 insStmt.setBinaryStream( 2, new ByteArrayInputStream (blob3), blob3.length); 107 insStmt.setAsciiStream( 3, new ByteArrayInputStream ( clob3), clob3.length); 108 insStmt.setAsciiStream( 4, new ByteArrayInputStream ( clob4), clob4.length); 109 insStmt.setBinaryStream( 5, new ByteArrayInputStream ( blob4), blob4.length); 110 insStmt.executeUpdate(); 111 conn.commit(); 112 insStmt.close(); 113 conn.close(); 114 } 115 catch( SQLException e) 116 { 117 dumpSQLExceptions(e); 118 } catch (Throwable e) { 119 System.out.println("FAIL -- unexpected exception:" + e.toString()); 120 } 121 122 try{ 124 TestUtil.getConnection("wombat", "shutdown=true"); 126 }catch(SQLException 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 e) { 132 System.out.println("FAIL -- unexpected exception:" + e.toString()); 133 } 134 135 System.out.println("testing rollforward recovery"); 136 try{ 137 Connection conn = TestUtil.getConnection("wombat", "rollForwardRecoveryFrom=extinout/mybackup/wombat"); 139 Statement stmt = conn.createStatement(); 141 stmt.execute("VALUES (ConsistencyChecker())"); 142 143 ResultSet 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 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 e) { 161 System.out.println("FAIL -- unexpected exception:" + e.toString()); 162 } 163 164 165 RandomAccessFile rfs = null; 168 boolean alreadyShutdown = false; 169 try{ 170 Connection conn = TestUtil.getConnection("wombat", null); 171 172 rfs = 175 new RandomAccessFile ( 176 "extinout/mybackup/wombat/service.properties" , "r"); 177 178 CallableStatement 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 se) 186 { 187 if (se.getSQLState() != null && se.getSQLState().equals("XSRS4")) 188 { alreadyShutdown = false; 189 }else 191 if (se.getSQLState() != null && 192 se.getSQLState().equals("08006")) 193 { System.out.println("database shutdown properly"); 197 alreadyShutdown = true; 198 }else 199 dumpSQLExceptions(se); 200 }catch (Throwable e) { 201 System.out.println("FAIL -- unexpected exception:" + e.toString()); 202 } 203 204 if(!alreadyShutdown) 206 { 207 try{ 208 TestUtil.getConnection("wombat", "shutdown=true"); 210 }catch(SQLException 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 e) { 216 System.out.println("FAIL -- unexpected exception:" + e.toString()); 217 } 218 } 219 220 try{ 222 if(rfs != null ) 224 rfs.close(); 225 226 Connection conn = TestUtil.getConnection("wombat", "restoreFrom=extinout/mybackup/wombat"); 227 228 Statement stmt = conn.createStatement(); 230 stmt.execute("VALUES (ConsistencyChecker())"); 231 conn.close(); 232 TestUtil.getConnection("wombat", "shutdown=true"); 234 }catch(SQLException 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 e) { 241 System.out.println("FAIL -- unexpected exception:" + e.toString()); 242 } 243 244 try{ 246 Connection conn = TestUtil.getConnection("wombat", null); 247 248 CallableStatement 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 stmt = conn.createStatement(); 255 stmt.execute("VALUES (ConsistencyChecker())"); 256 conn.close(); 257 TestUtil.getConnection("wombat", "shutdown=true"); 258 }catch(SQLException 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 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 se) { 274 System.out.println("FAIL -- unexpected exception: " + se.toString()); 275 SQLException 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 |