1 16 17 package org.apache.commons.dbcp.datasources; 18 19 import java.sql.Connection ; 20 import java.sql.PreparedStatement ; 21 import java.sql.ResultSet ; 22 import java.sql.SQLException ; 23 24 import javax.sql.DataSource ; 25 26 import junit.framework.Test; 27 import junit.framework.TestSuite; 28 29 import org.apache.commons.dbcp.TestConnectionPool; 30 import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS; 31 32 37 public class TestSharedPoolDataSource extends TestConnectionPool { 38 public TestSharedPoolDataSource(String testName) { 39 super(testName); 40 } 41 42 public static Test suite() { 43 return new TestSuite(TestSharedPoolDataSource.class); 44 } 45 46 protected Connection getConnection() throws Exception { 47 return ds.getConnection("foo","bar"); 48 } 49 50 private DriverAdapterCPDS pcds; 51 private DataSource ds; 52 53 public void setUp() throws Exception { 54 pcds = new DriverAdapterCPDS(); 55 pcds.setDriver("org.apache.commons.dbcp.TesterDriver"); 56 pcds.setUrl("jdbc:apache:commons:testdriver"); 57 pcds.setUser("foo"); 58 pcds.setPassword("bar"); 59 pcds.setPoolPreparedStatements(false); 60 61 SharedPoolDataSource tds = new SharedPoolDataSource(); 62 tds.setConnectionPoolDataSource(pcds); 63 tds.setMaxActive(getMaxActive()); 64 tds.setMaxWait((int)(getMaxWait())); 65 tds.setDefaultTransactionIsolation( 66 Connection.TRANSACTION_READ_COMMITTED); 67 68 ds = tds; 69 } 70 71 76 public void testIncorrectPassword() throws Exception 77 { 78 try { 79 ds.getConnection("u1", "zlsafjk").close(); 81 fail("Able to retrieve connection with incorrect password"); 82 } catch (SQLException e1) { 83 85 } 86 87 ds.getConnection("u1", "p1").close(); 89 try 90 { 91 ds.getConnection("u1", "x").close(); 92 fail("Able to retrieve connection with incorrect password"); 93 } 94 catch (SQLException e) 95 { 96 if (!e.getMessage().startsWith("Given password did not match")) 97 { 98 throw e; 99 } 100 } 102 103 ds.getConnection("u1", "p1").close(); 105 } 106 107 108 public void testSimple() throws Exception 109 { 110 Connection conn = ds.getConnection(); 111 assertTrue(null != conn); 112 PreparedStatement stmt = conn.prepareStatement("select * from dual"); 113 assertTrue(null != stmt); 114 ResultSet rset = stmt.executeQuery(); 115 assertTrue(null != rset); 116 assertTrue(rset.next()); 117 rset.close(); 118 stmt.close(); 119 conn.close(); 120 } 121 122 public void testSimpleWithUsername() throws Exception 123 { 124 Connection conn = ds.getConnection("u1", "p1"); 125 assertTrue(null != conn); 126 PreparedStatement stmt = conn.prepareStatement("select * from dual"); 127 assertTrue(null != stmt); 128 ResultSet rset = stmt.executeQuery(); 129 assertTrue(null != rset); 130 assertTrue(rset.next()); 131 rset.close(); 132 stmt.close(); 133 conn.close(); 134 } 135 136 public void testClosingWithUserName() 137 throws Exception 138 { 139 Connection [] c = new Connection [getMaxActive()]; 140 for (int i=0; i<c.length; i++) 142 { 143 c[i] = ds.getConnection("u1", "p1"); 144 } 145 146 c[0].close(); 148 assertTrue(c[0].isClosed()); 149 c[0] = ds.getConnection("u1", "p1"); 151 152 for (int i=0; i<c.length; i++) 153 { 154 c[i].close(); 155 } 156 157 for (int i=0; i<c.length; i++) 159 { 160 c[i] = ds.getConnection("u1", "p1"); 161 } 162 for (int i=0; i<c.length; i++) 163 { 164 c[i].close(); 165 } 166 } 167 168 public void testSimple2() 169 throws Exception 170 { 171 Connection conn = ds.getConnection(); 172 assertTrue(null != conn); 173 174 PreparedStatement stmt = 175 conn.prepareStatement("select * from dual"); 176 assertTrue(null != stmt); 177 ResultSet rset = stmt.executeQuery(); 178 assertTrue(null != rset); 179 assertTrue(rset.next()); 180 rset.close(); 181 stmt.close(); 182 183 stmt = conn.prepareStatement("select * from dual"); 184 assertTrue(null != stmt); 185 rset = stmt.executeQuery(); 186 assertTrue(null != rset); 187 assertTrue(rset.next()); 188 rset.close(); 189 stmt.close(); 190 191 conn.close(); 192 try 193 { 194 conn.createStatement(); 195 fail("Can't use closed connections"); 196 } 197 catch(SQLException e) 198 { 199 } 201 202 conn = ds.getConnection(); 203 assertTrue(null != conn); 204 205 stmt = conn.prepareStatement("select * from dual"); 206 assertTrue(null != stmt); 207 rset = stmt.executeQuery(); 208 assertTrue(null != rset); 209 assertTrue(rset.next()); 210 rset.close(); 211 stmt.close(); 212 213 stmt = conn.prepareStatement("select * from dual"); 214 assertTrue(null != stmt); 215 rset = stmt.executeQuery(); 216 assertTrue(null != rset); 217 assertTrue(rset.next()); 218 rset.close(); 219 stmt.close(); 220 221 conn.close(); 222 conn = null; 223 } 224 225 public void testOpening() 226 throws Exception 227 { 228 Connection [] c = new Connection [getMaxActive()]; 229 for (int i=0; i<c.length; i++) 231 { 232 c[i] = ds.getConnection(); 233 assertTrue(c[i] != null); 234 for (int j=0; j<=i; j++) 235 { 236 assertTrue(!c[j].isClosed()); 237 } 238 } 239 240 for (int i=0; i<c.length; i++) 241 { 242 c[i].close(); 243 } 244 } 245 246 public void testClosing() 247 throws Exception 248 { 249 Connection [] c = new Connection [getMaxActive()]; 250 for (int i=0; i<c.length; i++) 252 { 253 c[i] = ds.getConnection(); 254 } 255 256 c[0].close(); 258 assertTrue(c[0].isClosed()); 259 260 c[0] = ds.getConnection(); 262 263 for (int i=0; i<c.length; i++) 264 { 265 c[i].close(); 266 } 267 } 268 269 public void testMaxActive() 270 throws Exception 271 { 272 Connection [] c = new Connection [getMaxActive()]; 273 for (int i=0; i<c.length; i++) 274 { 275 c[i] = ds.getConnection(); 276 assertTrue(c[i] != null); 277 } 278 279 try 280 { 281 ds.getConnection(); 282 fail("Allowed to open more than DefaultMaxActive connections."); 283 } 284 catch(java.sql.SQLException e) 285 { 286 } 289 290 for (int i=0; i<c.length; i++) 291 { 292 c[i].close(); 293 } 294 } 295 296 public void testMultipleThreads() throws Exception { 297 assertTrue(multipleThreads(1)); 298 assertTrue(!multipleThreads(2 * (int)(getMaxWait()))); 299 } 300 301 private boolean multipleThreads(int holdTime) throws Exception { 302 long startTime = System.currentTimeMillis(); 303 final boolean[] success = new boolean[1]; 304 success[0] = true; 305 final PoolTest[] pts = new PoolTest[2 * getMaxActive()]; 306 ThreadGroup threadGroup = new ThreadGroup ("foo") { 307 public void uncaughtException(Thread t, Throwable e) { 308 314 for (int i = 0; i < pts.length; i++) { 315 pts[i].stop(); 316 } 317 318 success[0] = false; 320 } 321 }; 322 323 for (int i = 0; i < pts.length; i++) { 324 pts[i] = new PoolTest(threadGroup, holdTime); 325 } 326 Thread.sleep(10 * holdTime); 327 for (int i = 0; i < pts.length; i++) { 328 pts[i].stop(); 329 } 330 long time = System.currentTimeMillis() - startTime; 331 System.out.println("Multithread test time = " + time + " ms"); 333 334 Thread.sleep(holdTime); 335 return success[0]; 336 } 337 338 private static int currentThreadCount = 0; 339 340 private class PoolTest implements Runnable { 341 344 private int connHoldTime; 345 346 private boolean isRun; 347 348 private String state; 349 350 protected PoolTest(ThreadGroup threadGroup, int connHoldTime) { 351 this.connHoldTime = connHoldTime; 352 Thread thread = 353 new Thread (threadGroup, this, "Thread+" + currentThreadCount++); 354 thread.setDaemon(false); 355 thread.start(); 356 } 357 358 public void run() { 359 isRun = true; 360 while (isRun) { 361 try { 362 Connection conn = null; 363 state = "Getting Connection"; 364 conn = getConnection(); 365 state = "Using Connection"; 366 assertTrue(null != conn); 367 PreparedStatement stmt = 368 conn.prepareStatement("select * from dual"); 369 assertTrue(null != stmt); 370 ResultSet rset = stmt.executeQuery(); 371 assertTrue(null != rset); 372 assertTrue(rset.next()); 373 state = "Holding Connection"; 374 Thread.sleep(connHoldTime); 375 state = "Returning Connection"; 376 rset.close(); 377 stmt.close(); 378 conn.close(); 379 } catch (RuntimeException e) { 380 throw e; 381 } catch (Exception e) { 382 throw new RuntimeException (e.toString()); 383 } 384 } 385 } 386 387 public void stop() { 388 isRun = false; 389 } 390 391 public String reportState() { 392 return state; 393 } 394 } 395 396 public void testTransactionIsolationBehavior() throws Exception { 397 Connection conn = getConnection(); 398 assertTrue(conn != null); 399 assertEquals(Connection.TRANSACTION_READ_COMMITTED, 400 conn.getTransactionIsolation()); 401 conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); 402 conn.close(); 403 404 Connection conn2 = getConnection(); 405 assertEquals(Connection.TRANSACTION_READ_COMMITTED, 406 conn2.getTransactionIsolation()); 407 408 Connection conn3 = getConnection(); 409 assertEquals(Connection.TRANSACTION_READ_COMMITTED, 410 conn3.getTransactionIsolation()); 411 conn2.close(); 412 conn3.close(); 413 } 414 415 416 public void testPoolPrepareStatement() throws Exception 419 { 420 pcds.setPoolPreparedStatements(true); 421 422 Connection conn = ds.getConnection(); 423 assertTrue(null != conn); 424 PreparedStatement stmt = conn.prepareStatement("select * from dual"); 425 assertTrue(null != stmt); 426 ResultSet rset = stmt.executeQuery(); 427 assertTrue(null != rset); 428 assertTrue(rset.next()); 429 rset.close(); 430 stmt.close(); 431 conn.close(); 432 } 433 } 434 | Popular Tags |