1 21 22 package org.apache.derbyTesting.functionTests.tests.store; 23 import java.sql.Connection ; 24 import java.sql.Statement ; 25 import java.sql.PreparedStatement ; 26 import java.sql.ResultSet ; 27 import java.sql.SQLException ; 28 import org.apache.derby.tools.ij; 29 30 41 42 public class MaxLogNumber{ 43 44 MaxLogNumber() { 45 } 46 47 48 private void runTest(Connection conn) throws SQLException { 49 logMessage("Begin MaxLogNumber Test"); 50 performCheckPoint(conn); 54 createTable(conn); 55 insert(conn, 100, COMMIT, 10); 56 insert(conn, 100, ROLLBACK, 10); 57 update(conn, 50, COMMIT, 10); 58 update(conn, 50, ROLLBACK, 10); 59 verifyData(conn, 100); 60 insert(conn, 2000, NOACTION, 2000); 62 logMessage("End MaxLogNumber Test"); 63 } 64 65 void performCheckPoint(Connection conn) throws SQLException 66 { 67 Statement stmt = conn.createStatement(); 68 stmt.executeUpdate("CALL SYSCS_UTIL.SYSCS_CHECKPOINT_DATABASE()"); 70 stmt.close(); 71 } 72 73 74 77 void insert(Connection conn, int rowCount, 78 int txStatus, int commitCount) throws SQLException { 79 80 PreparedStatement ps = conn.prepareStatement("INSERT INTO " + 81 "emp" + 82 " VALUES(?,?,?)"); 83 for (int i = 0; i < rowCount; i++) { 84 85 ps.setInt(1, i); ps.setString(2 , "skywalker" + i); 87 ps.setFloat(3, (float)(i * 2000)); 88 ps.executeUpdate(); 89 if ((i % commitCount) == 0) 90 { 91 endTransaction(conn, txStatus); 92 } 93 } 94 95 endTransaction(conn, txStatus); 96 ps.close(); 97 } 98 99 100 static final int COMMIT = 1; 101 static final int ROLLBACK = 2; 102 static final int NOACTION = 3; 103 104 void endTransaction(Connection conn, int txStatus) throws SQLException 105 { 106 switch(txStatus){ 107 case COMMIT: 108 conn.commit(); 109 break; 110 case ROLLBACK: 111 conn.rollback(); 112 break; 113 case NOACTION: 114 break; 116 } 117 } 118 119 122 123 void update(Connection conn, int rowCount, 124 int txStatus, int commitCount) throws SQLException 125 { 126 127 PreparedStatement ps = conn.prepareStatement("update " + "emp" + 128 " SET salary=? where id=?"); 129 130 for (int i = 0; i < rowCount; i++) { 131 132 ps.setFloat(1, (float)(i * 2000 * 0.08)); 133 ps.setInt(2, i); ps.executeUpdate(); 135 if ((i % commitCount) == 0) 136 { 137 endTransaction(conn, txStatus); 138 } 139 } 140 endTransaction(conn, txStatus); 141 ps.close(); 142 } 143 144 145 148 void verifyData(Connection conn, int expectedRowCount) throws SQLException { 149 150 Statement s = conn.createStatement(); 151 ResultSet rs = s.executeQuery("SELECT ID, name from emp order by id" ); 152 int count = 0; 153 int id = 0; 154 while(rs.next()) 155 { 156 int tid = rs.getInt(1); 157 String name = rs.getString(2); 158 if(name.equals("skywalker" + id) && tid!= id) 159 { 160 161 logMessage("DATA IN THE TABLE IS NOT AS EXPECTED"); 162 logMessage("Got :ID=" + tid + " Name=:" + name); 163 logMessage("Expected: ID=" + id + "Name=" + "skywalker" + id ); 164 } 165 166 id++; 167 count++; 168 } 169 170 if(count != expectedRowCount) 171 { 172 logMessage("Expected Number Of Rows (" + 173 expectedRowCount + ")" + "!=" + 174 "No Of rows in the Table(" + 175 count + ")"); 176 } 177 s.close(); 178 } 179 180 183 void createTable(Connection conn) throws SQLException { 184 185 Statement s = conn.createStatement(); 186 s.executeUpdate("CREATE TABLE " + "emp" + 187 "(id INT," + 188 "name CHAR(200),"+ 189 "salary float)"); 190 s.executeUpdate("create index emp_idx on emp(id) "); 191 conn.commit(); 192 s.close(); 193 } 194 195 void logMessage(String str) 196 { 197 System.out.println(str); 198 } 199 200 201 public static void main(String [] argv) throws Throwable { 202 203 MaxLogNumber test = new MaxLogNumber(); 204 ij.getPropertyArg(argv); 205 Connection conn = ij.startJBMS(); 206 conn.setAutoCommit(false); 207 208 try { 209 test.runTest(conn); 210 } 211 catch (SQLException sqle) { 212 org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException( 213 System.out, sqle); 214 sqle.printStackTrace(System.out); 215 } 216 } 217 } 218 | Popular Tags |