1 24 25 package org.objectweb.cjdbc.scenario.tools.testlet; 26 27 import java.security.SecureRandom ; 28 import java.sql.Connection ; 29 import java.sql.PreparedStatement ; 30 import java.sql.SQLException ; 31 import java.util.ArrayList ; 32 import java.util.Properties ; 33 34 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase; 35 import org.objectweb.cjdbc.scenario.tools.ScenarioUtility; 36 37 43 public class PreparedStatementRecoveryTestLet extends AbstractVdbTestLet 44 { 45 46 SecureRandom rand = new SecureRandom (); 47 48 int numberOfUpdates; 49 50 55 public PreparedStatementRecoveryTestLet(VirtualDatabase vdb) 56 { 57 super(vdb); 58 config.put(NUMBER_OF_UPDATES, "50"); 59 config.put(USE_OPTIMIZED_STATEMENT, Boolean.TRUE); 60 61 } 62 63 66 public void execute() throws Exception 67 { 68 69 if (vdb.getAllBackendNames().size() < 2) 70 throw new TestLetException("This testlet needs at least two backends"); 71 72 Properties props = new Properties (); 73 boolean use = ((Boolean ) config.get(USE_OPTIMIZED_STATEMENT)) 74 .booleanValue(); 75 props.put("driverProcessed", "" + !use); 76 77 Connection con = getCJDBCConnection(props); 78 79 int numberOfUpdates = Integer.parseInt((String ) config 80 .get(NUMBER_OF_UPDATES)); 81 System.out.println("number of updates:" + numberOfUpdates); 82 83 String create = "create table setget (col1 varchar(10), col2 varchar(10),col3 varchar(10),col4 varchar(10),col5 varchar(10),col6 varchar(10),col7 varchar(10),col8 varchar(10), col9 varchar)"; 84 String insert = "insert into setget values(?,?,?,?,?,?,?,?,?)"; 85 con.createStatement().executeUpdate(create); 86 87 String checkpoint = "checkpoint" + System.currentTimeMillis(); 89 String backendName = getBackend(0).getName(); 90 vdb.disableBackendWithCheckpoint(backendName); 91 92 PreparedStatement ps = con.prepareStatement(insert); 94 for (int i = 0; i < numberOfUpdates; i++) 95 doExecuteRandomPStatement(ps, 9); 96 97 con.close(); 98 99 vdb.enableBackendFromCheckpoint(backendName); 101 107 String query = "select * from setget"; 109 ArrayList result1 = ScenarioUtility.getSingleQueryResult(query, 110 getBackendConnection(0)); 111 ScenarioUtility.displayResultOnScreen(result1); 112 ArrayList result2 = ScenarioUtility.getSingleQueryResult(query, 114 getBackendConnection(1)); 115 ScenarioUtility.displayResultOnScreen(result2); 116 117 assertEquals(result1, result2); 118 } 119 120 private void doExecuteRandomPStatement(PreparedStatement ps, int size) 121 throws SQLException 122 { 123 for (int i = 0; i < size; i++) 124 doSetRandomParameter(i + 1, ps); 125 126 int result = ps.executeUpdate(); 127 assertTrue("Updated columns returned false value", result == 1); 128 } 129 130 private void doSetRandomParameter(int index, PreparedStatement ps) 131 throws SQLException 132 { 133 int type = rand.nextInt(12); 134 if (type == 0) 135 ps.setBoolean(index, rand.nextBoolean()); 136 else if (type == 1) 137 ps.setByte(index, (byte) rand.nextInt()); 138 else if (type == 2) 139 { 140 byte[] b = new byte[10]; 141 rand.nextBytes(b); 142 ps.setBytes(index, b); 143 } 144 else if (type == 3) 145 ps.setDate(index, new java.sql.Date (2004, 10, 10)); 146 else if (type == 4) 147 ps.setDouble(index, rand.nextDouble()); 148 else if (type == 5) 149 ps.setFloat(index, rand.nextFloat()); 150 else if (type == 6) 151 ps.setInt(index, rand.nextInt(10)); 152 else if (type == 7) 153 ps.setLong(index, rand.nextLong()); 154 else if (type == 8) 155 ps.setNull(index, java.sql.Types.VARCHAR); 156 else if (type == 9) 157 ps.setObject(index, new String ("niko")); 158 else if (type == 10) 159 ps.setShort(index, (short) rand.nextInt(10)); 160 else if (type == 11) 161 ps.setString(index, "niko"); 162 } 165 166 } | Popular Tags |