001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.lang.stmtCache3
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.lang;
023:
024: import java.sql.Connection;
025: import java.sql.DriverManager;
026: import java.sql.Statement;
027: import java.sql.PreparedStatement;
028: import java.sql.ResultSet;
029: import java.sql.ResultSetMetaData;
030: import java.sql.SQLException;
031: import java.sql.SQLWarning;
032: import java.sql.Types;
033:
034: import org.apache.derby.tools.ij;
035: import org.apache.derby.tools.JDBCDisplayUtil;
036:
037: /**
038: * Test the statement cache with a 3-statement size.
039: *
040: * @author ames
041: */
042:
043: public class stmtCache3 {
044:
045: private static Connection conn;
046: private static boolean passed = false;
047:
048: public static void main(String[] args) {
049: System.out.println("Test stmtCache3 starting");
050:
051: try {
052: // use the ij utility to read the property file and
053: // make the initial connection.
054: ij.getPropertyArg(args);
055: conn = ij.startJBMS();
056:
057: conn.setAutoCommit(false);
058:
059: passed = setupTest(conn);
060:
061: // tests stop at first failure.
062:
063: passed = passed && testGrowsAndShrinks(conn);
064:
065: passed = passed && cleanupTest(conn);
066:
067: conn.commit();
068: conn.close();
069:
070: } catch (Throwable e) {
071: passed = false;
072: System.out.println("FAIL: exception thrown:");
073: JDBCDisplayUtil.ShowException(System.out, e);
074: }
075:
076: if (passed)
077: System.out.println("PASS");
078: System.out.println("Test stmtCache3 finished");
079: }
080:
081: //
082: // the tests
083: //
084:
085: /**
086: * Create some helper aliases for checking the cache state.
087: *
088: * @param conn the Connection
089: *
090: * @exception SQLException thrown on unexpected failure.
091: */
092: static boolean setupTest(Connection conn) throws SQLException {
093:
094: boolean passed = checkCache(conn, 0);
095:
096: return passed;
097: }
098:
099: /**
100: * Verify the cache state.
101: * @param conn the connection to check
102: * @param numInCache the number expected to be cached
103: * @exception SQLException thrown on failure
104: */
105: static boolean checkCache(Connection conn, int numInCache)
106: throws SQLException {
107: PreparedStatement ps = conn
108: .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 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
123: + " statements in cache");
124: return passed;
125: }
126:
127: /**
128: * Test that all non-closed prepared statements
129: * hang around, while closed ones disappear immediately,
130: * When the cache is at its limit.
131: *
132: * @param conn The Connection
133: *
134: * @return true if it succeeds, false if it doesn't
135: *
136: * @exception SQLException Thrown if some unexpected error happens
137: */
138: static boolean testGrowsAndShrinks(Connection conn)
139: throws SQLException {
140: boolean passed = true;
141:
142: PreparedStatement ps1, ps2, ps3, ps4, ps5;
143: ResultSet rs1, rs2, rs3, rs4, rs5;
144:
145: ps1 = conn.prepareStatement("values 1");
146: ps2 = conn.prepareStatement("values 2");
147: ps3 = conn.prepareStatement("values 3");
148: ps4 = conn.prepareStatement("values 4");
149: ps5 = conn.prepareStatement("values 5");
150:
151: passed = passed && checkCache(conn, 3);
152:
153: rs1 = ps1.executeQuery();
154: rs2 = ps2.executeQuery();
155: rs3 = ps3.executeQuery();
156: rs4 = ps4.executeQuery();
157: rs5 = ps5.executeQuery();
158:
159: passed = passed && checkCache(conn, 3);
160:
161: rs1.next();
162: rs2.next();
163: rs3.next();
164: rs4.next();
165: rs5.next();
166:
167: passed = passed && checkCache(conn, 3);
168:
169: // this closes all of the result sets,
170: // but the prepared statements are still open
171: rs1.next();
172: rs2.next();
173: rs3.next();
174: rs4.next();
175: rs5.next();
176:
177: passed = passed && checkCache(conn, 3);
178:
179: // this ensures all of the result sets are closed,
180: // but the prepared statements are still open
181: rs1.close();
182: rs2.close();
183: rs3.close();
184: rs4.close();
185: rs5.close();
186:
187: passed = passed && checkCache(conn, 3);
188:
189: // let one get away, the cache should shrink...
190: ps1.close();
191: passed = passed && checkCache(conn, 3);
192:
193: // let one more get away, the cache should shrink...
194: ps2.close();
195: passed = passed && checkCache(conn, 3);
196:
197: // let one more get away, the cache won't shrink this time...
198: ps3.close();
199: passed = passed && checkCache(conn, 3);
200:
201: // close the rest, the cache should stay three...
202: ps4.close();
203: ps5.close();
204: passed = passed && checkCache(conn, 3);
205:
206: return passed;
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 conn) throws SQLException {
220:
221: return true;
222: }
223:
224: // used by stmtcache 5 test
225: public static String findStatementInCacheById(String sql)
226: throws SQLException {
227: Connection conn = DriverManager
228: .getConnection("jdbc:default:connection");
229:
230: PreparedStatement ps = conn.prepareStatement(sql);
231:
232: PreparedStatement ps2 = conn
233: .prepareStatement("select SQL_TEXT from new org.apache.derby.diag.StatementCache() as ST where ID = ?");
234: ps2.setString(1, ps.toString());
235: ResultSet rs = ps2.executeQuery();
236: rs.next();
237: String ret = rs.getString(1);
238: rs.close();
239:
240: ps2.close();
241: ps.close();
242:
243: return ret;
244:
245: }
246: }
|