KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > store > MaxLogNumber


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.store.MaxLogNumber
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.store;
23 import java.sql.Connection JavaDoc;
24 import java.sql.Statement JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import org.apache.derby.tools.ij;
29
30 /*
31  * This class tests log writes to the transaction log files with large log file
32  * id's and does a setup to test recovery with large log file id's in
33  * MaxLogNumberRecovery.java test. Large log file id's are simulated using
34  * a debug flag 'testMaxLogFileNumber' in the log factory, this is enabled
35  * by setting derby.debug.true=testMaxLogFileNumber in the properties file.
36  * In Non debug mode, this tests just acts as a plain log recovery test.
37  *
38  * @author <a HREF="mailto:suresh.thalamati@gmail.com">Suresh Thalamati</a>
39  * @version 1.0
40  */

41
42 public class MaxLogNumber{
43
44     MaxLogNumber() {
45     }
46     
47
48     private void runTest(Connection JavaDoc conn) throws SQLException JavaDoc {
49         logMessage("Begin MaxLogNumber Test");
50         // perform a checkpoint otherwise recovery test will look at log1
51
// instead of the log number that gets by the testMaxLogFileNumber
52
// debug flags.
53
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         //do some inserts that will be rolled back by recovey
61
insert(conn, 2000, NOACTION, 2000);
62         logMessage("End MaxLogNumber Test");
63     }
64
65     void performCheckPoint(Connection JavaDoc conn) throws SQLException JavaDoc
66     {
67         Statement JavaDoc stmt = conn.createStatement();
68         //wait to make sure that checkpoint thread finished it's work
69
stmt.executeUpdate("CALL SYSCS_UTIL.SYSCS_CHECKPOINT_DATABASE()");
70         stmt.close();
71     }
72
73         
74     /**
75      * Insert some rows into the table.
76      */

77     void insert(Connection JavaDoc conn, int rowCount,
78                 int txStatus, int commitCount) throws SQLException JavaDoc {
79
80         PreparedStatement JavaDoc ps = conn.prepareStatement("INSERT INTO " +
81                                                      "emp" +
82                                                      " VALUES(?,?,?)");
83         for (int i = 0; i < rowCount; i++) {
84             
85             ps.setInt(1, i); // ID
86
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 JavaDoc conn, int txStatus) throws SQLException JavaDoc
105     {
106         switch(txStatus){
107         case COMMIT:
108             conn.commit();
109             break;
110         case ROLLBACK:
111             conn.rollback();
112             break;
113         case NOACTION:
114             //do nothing
115
break;
116         }
117     }
118         
119     /**
120      * update some rows in the table.
121      */

122
123     void update(Connection JavaDoc conn, int rowCount,
124                 int txStatus, int commitCount) throws SQLException JavaDoc
125     {
126
127         PreparedStatement JavaDoc 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); // ID
134
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     /*
146      * verify the rows in the table.
147      */

148     void verifyData(Connection JavaDoc conn, int expectedRowCount) throws SQLException JavaDoc {
149         
150         Statement JavaDoc s = conn.createStatement();
151         ResultSet JavaDoc 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 JavaDoc 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     /*
181      * create the tables that are used by this test.
182      */

183     void createTable(Connection JavaDoc conn) throws SQLException JavaDoc {
184
185         Statement JavaDoc 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 JavaDoc str)
196     {
197         System.out.println(str);
198     }
199     
200     
201     public static void main(String JavaDoc[] argv) throws Throwable JavaDoc {
202         
203         MaxLogNumber test = new MaxLogNumber();
204         ij.getPropertyArg(argv);
205         Connection JavaDoc conn = ij.startJBMS();
206         conn.setAutoCommit(false);
207
208         try {
209             test.runTest(conn);
210         }
211         catch (SQLException JavaDoc sqle) {
212             org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(
213                 System.out, sqle);
214             sqle.printStackTrace(System.out);
215         }
216     }
217 }
218
Popular Tags