1 16 17 package org.apache.commons.dbcp; 18 19 import java.sql.Connection ; 20 import java.sql.PreparedStatement ; 21 import java.sql.ResultSet ; 22 import java.sql.SQLException ; 23 import java.sql.Statement ; 24 25 import junit.framework.TestCase; 26 27 33 42 public abstract class TestConnectionPool extends TestCase { 43 public TestConnectionPool(String testName) { 44 super(testName); 45 } 46 47 public void setUp() throws Exception { 48 super.setUp(); 49 } 50 51 public void tearDown() throws Exception { 52 super.tearDown(); 53 } 54 55 protected abstract Connection getConnection() throws Exception ; 56 57 protected int getMaxActive() { 58 return 10; 59 } 60 61 protected long getMaxWait() { 62 return 100L; 63 } 64 65 67 protected String getUsername(Connection conn) throws SQLException { 68 Statement stmt = conn.createStatement(); 69 ResultSet rs = stmt.executeQuery("select username"); 70 if (rs.next()) { 71 return rs.getString(1); 72 } 73 return null; 74 } 75 76 78 public void testClearWarnings() throws Exception { 79 Connection [] c = new Connection [getMaxActive()]; 80 for (int i = 0; i < c.length; i++) { 81 c[i] = getConnection(); 82 assertTrue(c[i] != null); 83 84 c[i].prepareCall("warning"); 86 } 87 88 for (int i = 0; i < c.length; i++) { 89 assertNotNull(c[i].getWarnings()); 90 } 91 92 for (int i = 0; i < c.length; i++) { 93 c[i].close(); 94 } 95 96 for (int i = 0; i < c.length; i++) { 97 c[i] = getConnection(); 98 } 99 100 for (int i = 0; i < c.length; i++) { 101 assertNull(c[i].getWarnings()); 103 } 104 105 for (int i = 0; i < c.length; i++) { 106 c[i].close(); 107 } 108 } 109 110 public void testIsClosed() throws Exception { 111 for(int i=0;i<getMaxActive();i++) { 112 Connection conn = getConnection(); 113 assertTrue(null != conn); 114 assertTrue(!conn.isClosed()); 115 PreparedStatement stmt = conn.prepareStatement("select * from dual"); 116 assertTrue(null != stmt); 117 ResultSet rset = stmt.executeQuery(); 118 assertTrue(null != rset); 119 assertTrue(rset.next()); 120 rset.close(); 121 stmt.close(); 122 conn.close(); 123 assertTrue(conn.isClosed()); 124 } 125 } 126 127 public void testCantCloseConnectionTwice() throws Exception { 128 for(int i=0;i<getMaxActive();i++) { Connection conn = getConnection(); 130 assertTrue(null != conn); 131 assertTrue(!conn.isClosed()); 132 conn.close(); 133 assertTrue(conn.isClosed()); 134 try { 135 conn.close(); 136 fail("Expected SQLException on second attempt to close (" + conn.getClass().getName() + ")"); 137 } catch(SQLException e) { 138 } 140 assertTrue(conn.isClosed()); 141 } 142 } 143 144 public void testCantCloseStatementTwice() throws Exception { 145 Connection conn = getConnection(); 146 assertTrue(null != conn); 147 assertTrue(!conn.isClosed()); 148 for(int i=0;i<2;i++) { PreparedStatement stmt = conn.prepareStatement("select * from dual"); 150 assertTrue(null != stmt); 151 stmt.close(); 152 try { 153 stmt.close(); 154 fail("Expected SQLException on second attempt to close (" + stmt.getClass().getName() + ")"); 155 } catch(SQLException e) { 156 } 158 } 159 conn.close(); 160 } 161 162 public void testSimple() throws Exception { 163 Connection conn = getConnection(); 164 assertTrue(null != conn); 165 PreparedStatement stmt = conn.prepareStatement("select * from dual"); 166 assertTrue(null != stmt); 167 ResultSet rset = stmt.executeQuery(); 168 assertTrue(null != rset); 169 assertTrue(rset.next()); 170 rset.close(); 171 stmt.close(); 172 conn.close(); 173 } 174 175 public void testRepeatedBorrowAndReturn() throws Exception { 176 for(int i=0;i<100;i++) { 177 Connection conn = getConnection(); 178 assertTrue(null != conn); 179 PreparedStatement stmt = conn.prepareStatement("select * from dual"); 180 assertTrue(null != stmt); 181 ResultSet rset = stmt.executeQuery(); 182 assertTrue(null != rset); 183 assertTrue(rset.next()); 184 rset.close(); 185 stmt.close(); 186 conn.close(); 187 } 188 } 189 190 public void testSimple2() throws Exception { 191 Connection conn = getConnection(); 192 assertTrue(null != conn); 193 { 194 PreparedStatement stmt = conn.prepareStatement("select * from dual"); 195 assertTrue(null != stmt); 196 ResultSet rset = stmt.executeQuery(); 197 assertTrue(null != rset); 198 assertTrue(rset.next()); 199 rset.close(); 200 stmt.close(); 201 } 202 { 203 PreparedStatement stmt = conn.prepareStatement("select * from dual"); 204 assertTrue(null != stmt); 205 ResultSet rset = stmt.executeQuery(); 206 assertTrue(null != rset); 207 assertTrue(rset.next()); 208 rset.close(); 209 stmt.close(); 210 } 211 conn.close(); 212 try { 213 conn.createStatement(); 214 fail("Can't use closed connections"); 215 } catch(SQLException e) { 216 ; } 218 219 conn = getConnection(); 220 assertTrue(null != conn); 221 { 222 PreparedStatement stmt = conn.prepareStatement("select * from dual"); 223 assertTrue(null != stmt); 224 ResultSet rset = stmt.executeQuery(); 225 assertTrue(null != rset); 226 assertTrue(rset.next()); 227 rset.close(); 228 stmt.close(); 229 } 230 { 231 PreparedStatement stmt = conn.prepareStatement("select * from dual"); 232 assertTrue(null != stmt); 233 ResultSet rset = stmt.executeQuery(); 234 assertTrue(null != rset); 235 assertTrue(rset.next()); 236 rset.close(); 237 stmt.close(); 238 } 239 conn.close(); 240 conn = null; 241 } 242 243 public void testPooling() throws Exception { 244 Connection conn = getConnection(); 245 Connection underconn = null; 246 if(conn instanceof DelegatingConnection) { 247 underconn = ((DelegatingConnection)conn).getInnermostDelegate(); 248 } else { 249 return; } 251 assertTrue(underconn != null); 252 Connection conn2 = getConnection(); 253 Connection underconn2 = null; 254 if(conn2 instanceof DelegatingConnection) { 255 underconn2 = ((DelegatingConnection)conn2).getInnermostDelegate(); 256 } else { 257 return; } 259 assertTrue(underconn2 != null); 260 assertTrue(underconn != underconn2); 261 conn2.close(); 262 conn.close(); 263 Connection conn3 = getConnection(); 264 Connection underconn3 = null; 265 if(conn3 instanceof DelegatingConnection) { 266 underconn3 = ((DelegatingConnection)conn3).getInnermostDelegate(); 267 } else { 268 return; } 270 assertTrue( underconn3 == underconn || underconn3 == underconn2 ); 271 conn3.close(); 272 } 273 274 public void testAutoCommitBehavior() throws Exception { 275 Connection conn = getConnection(); 276 assertTrue(conn != null); 277 assertTrue(conn.getAutoCommit()); 278 conn.setAutoCommit(false); 279 conn.close(); 280 281 Connection conn2 = getConnection(); 282 assertTrue( conn2.getAutoCommit() ); 283 284 Connection conn3 = getConnection(); 285 assertTrue( conn3.getAutoCommit() ); 286 287 conn2.close(); 288 289 conn3.close(); 290 } 291 292 293 public void testConnectionsAreDistinct() throws Exception { 294 Connection [] conn = new Connection [getMaxActive()]; 295 for(int i=0;i<conn.length;i++) { 296 conn[i] = getConnection(); 297 for(int j=0;j<i;j++) { 298 assertTrue(conn[j] != conn[i]); 299 assertTrue(!conn[j].equals(conn[i])); 300 } 301 } 302 for(int i=0;i<conn.length;i++) { 303 conn[i].close(); 304 } 305 } 306 307 308 public void testOpening() throws Exception { 309 Connection [] c = new Connection [getMaxActive()]; 310 for (int i = 0; i < c.length; i++) { 312 c[i] = getConnection(); 313 assertTrue(c[i] != null); 314 for (int j = 0; j <= i; j++) { 315 assertTrue(!c[j].isClosed()); 316 } 317 } 318 319 for (int i = 0; i < c.length; i++) { 320 c[i].close(); 321 } 322 } 323 324 public void testClosing() throws Exception { 325 Connection [] c = new Connection [getMaxActive()]; 326 for (int i = 0; i < c.length; i++) { 328 c[i] = getConnection(); 329 } 330 331 c[0].close(); 333 assertTrue(c[0].isClosed()); 334 335 c[0] = getConnection(); 337 338 for (int i = 0; i < c.length; i++) { 339 c[i].close(); 340 } 341 } 342 343 public void testMaxActive() throws Exception { 344 Connection [] c = new Connection [getMaxActive()]; 345 for (int i = 0; i < c.length; i++) { 346 c[i] = getConnection(); 347 assertTrue(c[i] != null); 348 } 349 350 try { 351 getConnection(); 352 fail("Allowed to open more than DefaultMaxActive connections."); 353 } catch (java.sql.SQLException e) { 354 } 357 358 for (int i = 0; i < c.length; i++) { 359 c[i].close(); 360 } 361 } 362 363 public void testThreaded() { 364 TestThread[] threads = new TestThread[getMaxActive()]; 365 for(int i=0;i<threads.length;i++) { 366 threads[i] = new TestThread(50,50); 367 Thread t = new Thread (threads[i]); 368 t.start(); 369 } 370 for(int i=0;i<threads.length;i++) { 371 while(!(threads[i]).complete()) { 372 try { 373 Thread.sleep(100L); 374 } catch(Exception e) { 375 } 377 } 378 if(threads[i].failed()) { 379 fail(); 380 } 381 } 382 } 383 384 class TestThread implements Runnable { 385 java.util.Random _random = new java.util.Random (); 386 boolean _complete = false; 387 boolean _failed = false; 388 int _iter = 100; 389 int _delay = 50; 390 391 public TestThread() { 392 } 393 394 public TestThread(int iter) { 395 _iter = iter; 396 } 397 398 public TestThread(int iter, int delay) { 399 _iter = iter; 400 _delay = delay; 401 } 402 403 public boolean complete() { 404 return _complete; 405 } 406 407 public boolean failed() { 408 return _failed; 409 } 410 411 public void run() { 412 for(int i=0;i<_iter;i++) { 413 try { 414 Thread.sleep((long)_random.nextInt(_delay)); 415 } catch(Exception e) { 416 } 418 Connection conn = null; 419 PreparedStatement stmt = null; 420 ResultSet rset = null; 421 try { 422 conn = getConnection(); 423 stmt = conn.prepareStatement("select 'literal', SYSDATE from dual"); 424 rset = stmt.executeQuery(); 425 try { 426 Thread.sleep((long)_random.nextInt(_delay)); 427 } catch(Exception e) { 428 } 430 } catch(Exception e) { 431 e.printStackTrace(); 432 _failed = true; 433 _complete = true; 434 break; 435 } finally { 436 try { rset.close(); } catch(Exception e) { } 437 try { stmt.close(); } catch(Exception e) { } 438 try { conn.close(); } catch(Exception e) { } 439 } 440 } 441 _complete = true; 442 } 443 } 444 445 public void testPrepareStatementOptions() throws Exception 449 { 450 Connection conn = getConnection(); 451 assertTrue(null != conn); 452 PreparedStatement stmt = conn.prepareStatement("select * from dual", 453 ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 454 assertTrue(null != stmt); 455 ResultSet rset = stmt.executeQuery(); 456 assertTrue(null != rset); 457 assertTrue(rset.next()); 458 459 assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE, rset.getType()); 460 assertEquals(ResultSet.CONCUR_UPDATABLE, rset.getConcurrency()); 461 462 rset.close(); 463 stmt.close(); 464 conn.close(); 465 } 466 467 public void testNoRsetClose() throws Exception { 470 Connection conn = getConnection(); 471 assertNotNull(conn); 472 PreparedStatement stmt = conn.prepareStatement("test"); 473 assertNotNull(stmt); 474 ResultSet rset = stmt.getResultSet(); 475 assertNotNull(rset); 476 stmt.close(); 478 conn.close(); 479 } 480 481 public void testHashCode() throws Exception { 483 Connection conn1 = getConnection(); 484 assertNotNull(conn1); 485 Connection conn2 = getConnection(); 486 assertNotNull(conn2); 487 488 assertTrue(conn1.hashCode() != conn2.hashCode()); 489 } 490 } 491 | Popular Tags |