1 21 22 package org.apache.derbyTesting.functionTests.tests.lang; 23 24 import java.sql.Connection ; 25 import java.sql.DriverManager ; 26 import java.sql.Statement ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.ResultSetMetaData ; 30 import java.sql.SQLException ; 31 import java.sql.SQLWarning ; 32 import java.sql.Types ; 33 34 import org.apache.derby.tools.ij; 35 import org.apache.derby.tools.JDBCDisplayUtil; 36 37 42 43 44 public class stmtCache3 { 45 46 private static Connection conn; 47 private static boolean passed = false; 48 49 public static void main(String [] args) { 50 System.out.println("Test stmtCache3 starting"); 51 52 try { 53 ij.getPropertyArg(args); 56 conn = ij.startJBMS(); 57 58 conn.setAutoCommit(false); 59 60 passed = setupTest(conn); 61 62 64 passed = passed && testGrowsAndShrinks(conn); 65 66 passed = passed && cleanupTest(conn); 67 68 conn.commit(); 69 conn.close(); 70 71 } catch (Throwable 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 86 93 static boolean setupTest(Connection conn) throws SQLException { 94 95 96 boolean passed = checkCache(conn, 0); 97 98 return passed; 99 } 100 101 107 static boolean checkCache(Connection conn, int numInCache) throws SQLException { 108 PreparedStatement ps = conn.prepareStatement("select count(*) from new org.apache.derby.diag.StatementCache() AS SC_CONTENTS"); 109 110 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+" statements in cache"); 123 return passed; 124 } 125 126 137 static boolean testGrowsAndShrinks(Connection conn) 138 throws SQLException { 139 boolean passed = true; 140 141 PreparedStatement ps1, ps2, ps3, ps4, ps5; 142 ResultSet 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 rs1.next(); 171 rs2.next(); 172 rs3.next(); 173 rs4.next(); 174 rs5.next(); 175 176 passed = passed && checkCache(conn, 3); 177 178 rs1.close(); 181 rs2.close(); 182 rs3.close(); 183 rs4.close(); 184 rs5.close(); 185 186 passed = passed && checkCache(conn,3); 187 188 ps1.close(); 190 passed = passed && checkCache(conn,3); 191 192 ps2.close(); 194 passed = passed && checkCache(conn,3); 195 196 ps3.close(); 198 passed = passed && checkCache(conn,3); 199 200 ps4.close(); 202 ps5.close(); 203 passed = passed && checkCache(conn,3); 204 205 return passed; 206 } 207 208 209 218 219 static boolean cleanupTest(Connection conn) throws SQLException { 220 221 return true; 222 } 223 224 public static String findStatementInCacheById(String sql) throws SQLException 226 { 227 Connection conn = DriverManager.getConnection("jdbc:default:connection"); 228 229 PreparedStatement ps = conn.prepareStatement(sql); 230 231 PreparedStatement 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 rs = ps2.executeQuery(); 234 rs.next(); 235 String ret = rs.getString(1); 236 rs.close(); 237 238 ps2.close(); 239 ps.close(); 240 241 return ret; 242 243 } 244 } 245 | Popular Tags |