001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.store.MaxLogNumber
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.Statement;
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028: import java.sql.SQLException;
029: import org.apache.derby.tools.ij;
030:
031: /*
032: * This class tests log writes to the transaction log files with large log file
033: * id's and does a setup to test recovery with large log file id's in
034: * MaxLogNumberRecovery.java test. Large log file id's are simulated using
035: * a debug flag 'testMaxLogFileNumber' in the log factory, this is enabled
036: * by setting derby.debug.true=testMaxLogFileNumber in the properties file.
037: * In Non debug mode, this tests just acts as a plain log recovery test.
038: *
039: * @author <a href="mailto:suresh.thalamati@gmail.com">Suresh Thalamati</a>
040: * @version 1.0
041: */
042:
043: public class MaxLogNumber {
044:
045: MaxLogNumber() {
046: }
047:
048: private void runTest(Connection conn) throws SQLException {
049: logMessage("Begin MaxLogNumber Test");
050: // perform a checkpoint otherwise recovery test will look at log1
051: // instead of the log number that gets by the testMaxLogFileNumber
052: // debug flags.
053: performCheckPoint(conn);
054: createTable(conn);
055: insert(conn, 100, COMMIT, 10);
056: insert(conn, 100, ROLLBACK, 10);
057: update(conn, 50, COMMIT, 10);
058: update(conn, 50, ROLLBACK, 10);
059: verifyData(conn, 100);
060: //do some inserts that will be rolled back by recovey
061: insert(conn, 2000, NOACTION, 2000);
062: logMessage("End MaxLogNumber Test");
063: }
064:
065: void performCheckPoint(Connection conn) throws SQLException {
066: Statement stmt = conn.createStatement();
067: //wait to make sure that checkpoint thread finished it's work
068: stmt
069: .executeUpdate("CALL SYSCS_UTIL.SYSCS_CHECKPOINT_DATABASE()");
070: stmt.close();
071: }
072:
073: /**
074: * Insert some rows into the table.
075: */
076: void insert(Connection conn, int rowCount, int txStatus,
077: int commitCount) throws SQLException {
078:
079: PreparedStatement ps = conn.prepareStatement("INSERT INTO "
080: + "emp" + " VALUES(?,?,?)");
081: for (int i = 0; i < rowCount; i++) {
082:
083: ps.setInt(1, i); // ID
084: ps.setString(2, "skywalker" + i);
085: ps.setFloat(3, (float) (i * 2000));
086: ps.executeUpdate();
087: if ((i % commitCount) == 0) {
088: endTransaction(conn, txStatus);
089: }
090: }
091:
092: endTransaction(conn, txStatus);
093: ps.close();
094: }
095:
096: static final int COMMIT = 1;
097: static final int ROLLBACK = 2;
098: static final int NOACTION = 3;
099:
100: void endTransaction(Connection conn, int txStatus)
101: throws SQLException {
102: switch (txStatus) {
103: case COMMIT:
104: conn.commit();
105: break;
106: case ROLLBACK:
107: conn.rollback();
108: break;
109: case NOACTION:
110: //do nothing
111: break;
112: }
113: }
114:
115: /**
116: * update some rows in the table.
117: */
118:
119: void update(Connection conn, int rowCount, int txStatus,
120: int commitCount) throws SQLException {
121:
122: PreparedStatement ps = conn.prepareStatement("update " + "emp"
123: + " SET salary=? where id=?");
124:
125: for (int i = 0; i < rowCount; i++) {
126:
127: ps.setFloat(1, (float) (i * 2000 * 0.08));
128: ps.setInt(2, i); // ID
129: ps.executeUpdate();
130: if ((i % commitCount) == 0) {
131: endTransaction(conn, txStatus);
132: }
133: }
134: endTransaction(conn, txStatus);
135: ps.close();
136: }
137:
138: /*
139: * verify the rows in the table.
140: */
141: void verifyData(Connection conn, int expectedRowCount)
142: throws SQLException {
143:
144: Statement s = conn.createStatement();
145: ResultSet rs = s
146: .executeQuery("SELECT ID, name from emp order by id");
147: int count = 0;
148: int id = 0;
149: while (rs.next()) {
150: int tid = rs.getInt(1);
151: String name = rs.getString(2);
152: if (name.equals("skywalker" + id) && tid != id) {
153:
154: logMessage("DATA IN THE TABLE IS NOT AS EXPECTED");
155: logMessage("Got :ID=" + tid + " Name=:" + name);
156: logMessage("Expected: ID=" + id + "Name=" + "skywalker"
157: + id);
158: }
159:
160: id++;
161: count++;
162: }
163:
164: if (count != expectedRowCount) {
165: logMessage("Expected Number Of Rows (" + expectedRowCount
166: + ")" + "!=" + "No Of rows in the Table(" + count
167: + ")");
168: }
169: s.close();
170: }
171:
172: /*
173: * create the tables that are used by this test.
174: */
175: void createTable(Connection conn) throws SQLException {
176:
177: Statement s = conn.createStatement();
178: s.executeUpdate("CREATE TABLE " + "emp" + "(id INT,"
179: + "name CHAR(200)," + "salary float)");
180: s.executeUpdate("create index emp_idx on emp(id) ");
181: conn.commit();
182: s.close();
183: }
184:
185: void logMessage(String str) {
186: System.out.println(str);
187: }
188:
189: public static void main(String[] argv) throws Throwable {
190:
191: MaxLogNumber test = new MaxLogNumber();
192: ij.getPropertyArg(argv);
193: Connection conn = ij.startJBMS();
194: conn.setAutoCommit(false);
195:
196: try {
197: test.runTest(conn);
198: } catch (SQLException sqle) {
199: org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(
200: System.out, sqle);
201: sqle.printStackTrace(System.out);
202: }
203: }
204: }
|