KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > stmtCache3


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.stmtCache3
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.lang;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.ResultSetMetaData JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.sql.SQLWarning JavaDoc;
32 import java.sql.Types JavaDoc;
33
34 import org.apache.derby.tools.ij;
35 import org.apache.derby.tools.JDBCDisplayUtil;
36
37 /**
38  * Test the statement cache with a 3-statement size.
39  *
40  * @author ames
41  */

42
43
44 public class stmtCache3 {
45
46     private static Connection JavaDoc conn;
47     private static boolean passed = false;
48
49     public static void main(String JavaDoc[] args) {
50         System.out.println("Test stmtCache3 starting");
51
52         try {
53             // use the ij utility to read the property file and
54
// make the initial connection.
55
ij.getPropertyArg(args);
56             conn = ij.startJBMS();
57
58             conn.setAutoCommit(false);
59
60             passed = setupTest(conn);
61
62             // tests stop at first failure.
63

64             passed = passed && testGrowsAndShrinks(conn);
65
66             passed = passed && cleanupTest(conn);
67
68             conn.commit();
69             conn.close();
70
71         } catch (Throwable JavaDoc e) {
72             passed = false;
73             System.out.println("FAIL: exception thrown:");
74             JDBCDisplayUtil.ShowException(System.out,e);
75         }
76
77         if (passed)
78             System.out.println("PASS");
79         System.out.println("Test stmtCache3 finished");
80     }
81
82     //
83
// the tests
84
//
85

86     /**
87      * Create some helper aliases for checking the cache state.
88      *
89      * @param conn the Connection
90      *
91      * @exception SQLException thrown on unexpected failure.
92      */

93     static boolean setupTest(Connection JavaDoc conn) throws SQLException JavaDoc {
94
95
96         boolean passed = checkCache(conn, 0);
97
98         return passed;
99     }
100
101     /**
102      * Verify the cache state.
103      * @param conn the connection to check
104      * @param numInCache the number expected to be cached
105      * @exception SQLException thrown on failure
106      */

107     static boolean checkCache(Connection JavaDoc conn, int numInCache) throws SQLException JavaDoc {
108         PreparedStatement JavaDoc ps = conn.prepareStatement("select count(*) from new org.apache.derby.diag.StatementCache() AS SC_CONTENTS");
109
110         // we're adding one with this statement, so account for it.
111
int actualNum = numInCache + 1;
112         if (actualNum > 3)
113             actualNum = 3;
114
115         ResultSet JavaDoc rs = ps.executeQuery();
116         rs.next();
117         boolean passed = rs.getInt(1) == actualNum;
118         rs.close();
119         ps.close();
120
121         if (!passed)
122           System.out.println("FAIL -- expected "+numInCache+" statements in cache");
123         return passed;
124     }
125
126     /**
127      * Test that all non-closed prepared statements
128      * hang around, while closed ones disappear immediately,
129      * When the cache is at its limit.
130      *
131      * @param conn The Connection
132      *
133      * @return true if it succeeds, false if it doesn't
134      *
135      * @exception SQLException Thrown if some unexpected error happens
136      */

137     static boolean testGrowsAndShrinks(Connection JavaDoc conn)
138                     throws SQLException JavaDoc {
139         boolean passed = true;
140
141         PreparedStatement JavaDoc ps1, ps2, ps3, ps4, ps5;
142         ResultSet JavaDoc rs1, rs2, rs3, rs4, rs5;
143
144         ps1 = conn.prepareStatement("values 1");
145         ps2 = conn.prepareStatement("values 2");
146         ps3 = conn.prepareStatement("values 3");
147         ps4 = conn.prepareStatement("values 4");
148         ps5 = conn.prepareStatement("values 5");
149
150         passed = passed && checkCache(conn,3);
151
152         rs1 = ps1.executeQuery();
153         rs2 = ps2.executeQuery();
154         rs3 = ps3.executeQuery();
155         rs4 = ps4.executeQuery();
156         rs5 = ps5.executeQuery();
157
158         passed = passed && checkCache(conn,3);
159
160         rs1.next();
161         rs2.next();
162         rs3.next();
163         rs4.next();
164         rs5.next();
165
166         passed = passed && checkCache(conn, 3);
167
168         // this closes all of the result sets,
169
// but the prepared statements are still open
170
rs1.next();
171         rs2.next();
172         rs3.next();
173         rs4.next();
174         rs5.next();
175
176         passed = passed && checkCache(conn, 3);
177
178         // this ensures all of the result sets are closed,
179
// but the prepared statements are still open
180
rs1.close();
181         rs2.close();
182         rs3.close();
183         rs4.close();
184         rs5.close();
185
186         passed = passed && checkCache(conn,3);
187
188         // let one get away, the cache should shrink...
189
ps1.close();
190         passed = passed && checkCache(conn,3);
191
192         // let one more get away, the cache should shrink...
193
ps2.close();
194         passed = passed && checkCache(conn,3);
195
196         // let one more get away, the cache won't shrink this time...
197
ps3.close();
198         passed = passed && checkCache(conn,3);
199
200         // close the rest, the cache should stay three...
201
ps4.close();
202         ps5.close();
203         passed = passed && checkCache(conn,3);
204
205         return passed;
206     }
207
208
209     /**
210      * Clean up after ourselves when testing is done.
211      *
212      * @param conn The Connection
213      *
214      * @return true if it succeeds, false if it doesn't
215      *
216      * @exception SQLException Thrown if some unexpected error happens
217      */

218
219     static boolean cleanupTest(Connection JavaDoc conn) throws SQLException JavaDoc {
220
221         return true;
222     }
223  
224     // used by stmtcache 5 test
225
public static String JavaDoc findStatementInCacheById(String JavaDoc sql) throws SQLException JavaDoc
226     {
227         Connection JavaDoc conn = DriverManager.getConnection("jdbc:default:connection");
228
229         PreparedStatement JavaDoc ps = conn.prepareStatement(sql);
230
231         PreparedStatement JavaDoc ps2 = conn.prepareStatement("select SQL_TEXT from new org.apache.derby.diag.StatementCache() as ST where ID = ?");
232         ps2.setString(1, ps.toString());
233         ResultSet JavaDoc rs = ps2.executeQuery();
234         rs.next();
235         String JavaDoc ret = rs.getString(1);
236         rs.close();
237
238         ps2.close();
239         ps.close();
240
241         return ret;
242
243     }
244 }
245
Popular Tags