1 25 package testsuite.regression; 26 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.SQLException ; 30 31 import javax.sql.ConnectionEvent ; 32 import javax.sql.ConnectionEventListener ; 33 import javax.sql.ConnectionPoolDataSource ; 34 import javax.sql.PooledConnection ; 35 36 import junit.framework.Test; 37 import junit.framework.TestSuite; 38 import junit.textui.TestRunner; 39 import testsuite.BaseTestCase; 40 import testsuite.simple.BlobTest; 41 42 import com.mysql.jdbc.PacketTooBigException; 43 import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource; 44 45 114 public final class PooledConnectionRegressionTest extends BaseTestCase { 115 private ConnectionPoolDataSource cpds; 116 117 private int closeEventCount; 119 120 private int connectionErrorEventCount; 122 123 129 public PooledConnectionRegressionTest(String testname) { 130 super(testname); 131 } 132 133 139 public void setUp() throws Exception { 140 super.setUp(); 141 142 this.closeEventCount = 0; 144 this.connectionErrorEventCount = 0; 145 146 MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource(); 147 148 ds.setURL(BaseTestCase.dbUrl); 149 150 cpds = ds; 151 } 152 153 158 public static void main(String [] args) { 159 junit.textui.TestRunner.run(PooledConnectionRegressionTest.class); 160 } 161 162 167 public static Test suite() { 168 TestSuite suite = new TestSuite(PooledConnectionRegressionTest.class); 169 170 return suite; 171 } 172 173 176 public void tearDown() { 177 cpds = null; 178 } 179 180 184 public void testBug7136() { 185 final ConnectionEventListener conListener = new ConnectionListener(); 186 PooledConnection pc = null; 187 this.closeEventCount = 0; 188 189 try { 190 pc = cpds.getPooledConnection(); 191 192 pc.addConnectionEventListener(conListener); 193 194 Connection conn = pc.getConnection(); 195 196 Connection connFromStatement = conn.createStatement() 197 .getConnection(); 198 199 201 connFromStatement.close(); 202 203 assertEquals("One close event should've been registered", 1, 204 this.closeEventCount); 205 206 this.closeEventCount = 0; 207 208 conn = pc.getConnection(); 209 210 Connection connFromPreparedStatement = conn.prepareStatement( 211 "SELECT 1").getConnection(); 212 213 215 connFromPreparedStatement.close(); 216 217 assertEquals("One close event should've been registered", 1, 218 this.closeEventCount); 219 220 } catch (SQLException ex) { 221 fail(ex.toString()); 222 } finally { 223 if (pc != null) { 224 try { 225 pc.close(); 226 } catch (SQLException ex) { 227 ex.printStackTrace(); 228 } 229 } 230 } 231 } 232 233 237 public void testConnectionReclaim() { 238 final ConnectionEventListener conListener = new ConnectionListener(); 239 PooledConnection pc = null; 240 final int NB_TESTS = 5; 241 242 try { 243 pc = cpds.getPooledConnection(); 244 245 pc.addConnectionEventListener(conListener); 246 247 for (int i = 0; i < NB_TESTS; i++) { 248 Connection conn = pc.getConnection(); 249 250 try { 251 System.out.println("Before connection reclaim."); 253 254 conn = pc.getConnection(); 255 256 System.out.println("After connection reclaim."); 257 } finally { 258 if (conn != null) { 259 System.out.println("Before connection.close()."); 260 261 conn.close(); 263 264 System.out.println("After connection.close()."); 265 } 266 } 267 } 268 } catch (SQLException ex) { 269 ex.printStackTrace(); 270 fail(ex.toString()); 271 } finally { 272 if (pc != null) { 273 try { 274 System.out.println("Before pooledConnection.close()."); 275 276 pc.close(); 278 279 System.out.println("After pooledConnection.close()."); 280 } catch (SQLException ex) { 281 ex.printStackTrace(); 282 fail(ex.toString()); 283 } 284 } 285 } 286 287 assertEquals("Wrong nb of CloseEvents: ", NB_TESTS, closeEventCount); 288 } 289 290 296 public void testPacketTooLargeException() throws Exception { 297 final ConnectionEventListener conListener = new ConnectionListener(); 298 PooledConnection pc = null; 299 300 pc = cpds.getPooledConnection(); 301 302 pc.addConnectionEventListener(conListener); 303 304 try { 305 this.stmt.executeUpdate("DROP TABLE IF EXISTS testPacketTooLarge"); 306 this.stmt 307 .executeUpdate("CREATE TABLE testPacketTooLarge(field1 LONGBLOB)"); 308 309 Connection connFromPool = pc.getConnection(); 310 PreparedStatement pStmtFromPool = connFromPool 311 .prepareStatement("INSERT INTO testPacketTooLarge VALUES (?)"); 312 this.rs = this.stmt 313 .executeQuery("SHOW VARIABLES LIKE 'max_allowed_packet'"); 314 this.rs.next(); 315 316 int maxAllowedPacket = this.rs.getInt(2); 317 318 byte[] bigBytes = new byte[(int) (maxAllowedPacket * 1.2)]; 319 320 try { 321 pStmtFromPool.setBytes(1, bigBytes); 322 pStmtFromPool.executeUpdate(); 323 fail("Expecting PacketTooLargeException"); 324 } catch (PacketTooBigException ptbe) { 325 } 327 328 connFromPool.createStatement().executeQuery("SELECT 1"); 332 333 assertTrue(this.connectionErrorEventCount == 0); 334 assertTrue(this.closeEventCount == 0); 335 } finally { 336 this.stmt.executeUpdate("DROP TABLE IF EXISTS testPacketTooLarge"); 337 } 338 } 339 340 345 public void testCloseEvent() { 346 final ConnectionEventListener conListener = new ConnectionListener(); 347 PooledConnection pc = null; 348 final int NB_TESTS = 5; 349 350 try { 351 pc = this.cpds.getPooledConnection(); 352 353 pc.addConnectionEventListener(conListener); 354 355 for (int i = 0; i < NB_TESTS; i++) { 356 Connection pConn = pc.getConnection(); 357 358 System.out.println("Before connection.close()."); 359 360 pConn.close(); 362 363 System.out.println("After connection.close()."); 364 } 365 } catch (SQLException ex) { 366 fail(ex.toString()); 367 } finally { 368 if (pc != null) { 369 try { 370 System.out.println("Before pooledConnection.close()."); 371 372 pc.close(); 374 375 System.out.println("After pooledConnection.close()."); 376 } catch (SQLException ex) { 377 ex.printStackTrace(); 378 } 379 } 380 } 381 assertEquals("Wrong nb of CloseEvents: ", NB_TESTS, 382 this.closeEventCount); 383 } 384 385 388 private final class ConnectionListener implements ConnectionEventListener { 389 390 public void connectionClosed(ConnectionEvent event) { 391 closeEventCount++; 392 System.out.println(closeEventCount + " - Connection closed."); 393 } 394 395 396 public void connectionErrorOccurred(ConnectionEvent event) { 397 connectionErrorEventCount++; 398 System.out.println("Connection error: " + event.getSQLException()); 399 } 400 } 401 } 402 | Popular Tags |