KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbcp > TestConnectionPool


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.dbcp;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Statement JavaDoc;
24
25 import junit.framework.TestCase;
26
27 // XXX FIX ME XXX
28
// this class still needs some cleanup, but at least
29
// this consolidates most of the relevant test code
30
// in a fairly re-usable fashion
31
// XXX FIX ME XXX
32

33 /**
34  * Base test suite for DBCP pools.
35  *
36  * @author Rodney Waldhoff
37  * @author Sean C. Sullivan
38  * @author John McNally
39  * @author Dirk Verbeeck
40  * @version $Revision: 1.14 $ $Date: 2004/02/28 12:18:18 $
41  */

42 public abstract class TestConnectionPool extends TestCase {
43     public TestConnectionPool(String JavaDoc testName) {
44         super(testName);
45     }
46
47     public void setUp() throws Exception JavaDoc {
48         super.setUp();
49     }
50
51     public void tearDown() throws Exception JavaDoc {
52         super.tearDown();
53     }
54
55     protected abstract Connection JavaDoc getConnection() throws Exception JavaDoc;
56     
57     protected int getMaxActive() {
58         return 10;
59     }
60     
61     protected long getMaxWait() {
62         return 100L;
63     }
64
65     // ----------- Utility Methods ---------------------------------
66

67     protected String JavaDoc getUsername(Connection JavaDoc conn) throws SQLException JavaDoc {
68         Statement JavaDoc stmt = conn.createStatement();
69         ResultSet JavaDoc rs = stmt.executeQuery("select username");
70         if (rs.next()) {
71             return rs.getString(1);
72         }
73         return null;
74     }
75
76     // ----------- tests ---------------------------------
77

78     public void testClearWarnings() throws Exception JavaDoc {
79         Connection JavaDoc[] c = new Connection JavaDoc[getMaxActive()];
80         for (int i = 0; i < c.length; i++) {
81             c[i] = getConnection();
82             assertTrue(c[i] != null);
83             
84             // generate SQLWarning on connection
85
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             // warnings should have been cleared by putting the connection back in the pool
102
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 JavaDoc {
111         for(int i=0;i<getMaxActive();i++) {
112             Connection JavaDoc conn = getConnection();
113             assertTrue(null != conn);
114             assertTrue(!conn.isClosed());
115             PreparedStatement JavaDoc stmt = conn.prepareStatement("select * from dual");
116             assertTrue(null != stmt);
117             ResultSet JavaDoc 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 JavaDoc {
128         for(int i=0;i<getMaxActive();i++) { // loop to show we *can* close again once we've borrowed it from the pool again
129
Connection JavaDoc 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 JavaDoc e) {
138                 // expected
139
}
140             assertTrue(conn.isClosed());
141         }
142     }
143
144     public void testCantCloseStatementTwice() throws Exception JavaDoc {
145         Connection JavaDoc conn = getConnection();
146         assertTrue(null != conn);
147         assertTrue(!conn.isClosed());
148         for(int i=0;i<2;i++) { // loop to show we *can* close again once we've borrowed it from the pool again
149
PreparedStatement JavaDoc 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 JavaDoc e) {
156                 // expected
157
}
158         }
159         conn.close();
160     }
161
162     public void testSimple() throws Exception JavaDoc {
163         Connection JavaDoc conn = getConnection();
164         assertTrue(null != conn);
165         PreparedStatement JavaDoc stmt = conn.prepareStatement("select * from dual");
166         assertTrue(null != stmt);
167         ResultSet JavaDoc 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 JavaDoc {
176         for(int i=0;i<100;i++) {
177             Connection JavaDoc conn = getConnection();
178             assertTrue(null != conn);
179             PreparedStatement JavaDoc stmt = conn.prepareStatement("select * from dual");
180             assertTrue(null != stmt);
181             ResultSet JavaDoc 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 JavaDoc {
191         Connection JavaDoc conn = getConnection();
192         assertTrue(null != conn);
193         {
194             PreparedStatement JavaDoc stmt = conn.prepareStatement("select * from dual");
195             assertTrue(null != stmt);
196             ResultSet JavaDoc rset = stmt.executeQuery();
197             assertTrue(null != rset);
198             assertTrue(rset.next());
199             rset.close();
200             stmt.close();
201         }
202         {
203             PreparedStatement JavaDoc stmt = conn.prepareStatement("select * from dual");
204             assertTrue(null != stmt);
205             ResultSet JavaDoc 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 JavaDoc e) {
216             ; // expected
217
}
218
219         conn = getConnection();
220         assertTrue(null != conn);
221         {
222             PreparedStatement JavaDoc stmt = conn.prepareStatement("select * from dual");
223             assertTrue(null != stmt);
224             ResultSet JavaDoc rset = stmt.executeQuery();
225             assertTrue(null != rset);
226             assertTrue(rset.next());
227             rset.close();
228             stmt.close();
229         }
230         {
231             PreparedStatement JavaDoc stmt = conn.prepareStatement("select * from dual");
232             assertTrue(null != stmt);
233             ResultSet JavaDoc 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 JavaDoc {
244         Connection JavaDoc conn = getConnection();
245         Connection JavaDoc underconn = null;
246         if(conn instanceof DelegatingConnection) {
247             underconn = ((DelegatingConnection)conn).getInnermostDelegate();
248         } else {
249             return; // skip this test
250
}
251         assertTrue(underconn != null);
252         Connection JavaDoc conn2 = getConnection();
253         Connection JavaDoc underconn2 = null;
254         if(conn2 instanceof DelegatingConnection) {
255             underconn2 = ((DelegatingConnection)conn2).getInnermostDelegate();
256         } else {
257             return; // skip this test
258
}
259         assertTrue(underconn2 != null);
260         assertTrue(underconn != underconn2);
261         conn2.close();
262         conn.close();
263         Connection JavaDoc conn3 = getConnection();
264         Connection JavaDoc underconn3 = null;
265         if(conn3 instanceof DelegatingConnection) {
266             underconn3 = ((DelegatingConnection)conn3).getInnermostDelegate();
267         } else {
268             return; // skip this test
269
}
270         assertTrue( underconn3 == underconn || underconn3 == underconn2 );
271         conn3.close();
272     }
273     
274     public void testAutoCommitBehavior() throws Exception JavaDoc {
275         Connection JavaDoc conn = getConnection();
276         assertTrue(conn != null);
277         assertTrue(conn.getAutoCommit());
278         conn.setAutoCommit(false);
279         conn.close();
280         
281         Connection JavaDoc conn2 = getConnection();
282         assertTrue( conn2.getAutoCommit() );
283         
284         Connection JavaDoc conn3 = getConnection();
285         assertTrue( conn3.getAutoCommit() );
286
287         conn2.close();
288         
289         conn3.close();
290     }
291     
292     /** @see http://issues.apache.org/bugzilla/show_bug.cgi?id=12400 */
293     public void testConnectionsAreDistinct() throws Exception JavaDoc {
294         Connection JavaDoc[] conn = new Connection JavaDoc[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 JavaDoc {
309         Connection JavaDoc[] c = new Connection JavaDoc[getMaxActive()];
310         // test that opening new connections is not closing previous
311
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 JavaDoc {
325         Connection JavaDoc[] c = new Connection JavaDoc[getMaxActive()];
326         // open the maximum connections
327
for (int i = 0; i < c.length; i++) {
328             c[i] = getConnection();
329         }
330
331         // close one of the connections
332
c[0].close();
333         assertTrue(c[0].isClosed());
334
335         // get a new connection
336
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 JavaDoc {
344         Connection JavaDoc[] c = new Connection JavaDoc[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 JavaDoc e) {
354             // should only be able to open 10 connections, so this test should
355
// throw an exception
356
}
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 JavaDoc t = new Thread JavaDoc(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 JavaDoc e) {
375                     // ignored
376
}
377             }
378             if(threads[i].failed()) {
379                 fail();
380             }
381         }
382     }
383
384     class TestThread implements Runnable JavaDoc {
385         java.util.Random JavaDoc _random = new java.util.Random JavaDoc();
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 JavaDoc e) {
416                     // ignored
417
}
418                 Connection JavaDoc conn = null;
419                 PreparedStatement JavaDoc stmt = null;
420                 ResultSet JavaDoc 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 JavaDoc e) {
428                         // ignored
429
}
430                 } catch(Exception JavaDoc e) {
431                     e.printStackTrace();
432                     _failed = true;
433                     _complete = true;
434                     break;
435                 } finally {
436                     try { rset.close(); } catch(Exception JavaDoc e) { }
437                     try { stmt.close(); } catch(Exception JavaDoc e) { }
438                     try { conn.close(); } catch(Exception JavaDoc e) { }
439                 }
440             }
441             _complete = true;
442         }
443     }
444
445     // Bugzilla Bug 24328: PooledConnectionImpl ignores resultsetType
446
// and Concurrency if statement pooling is not enabled
447
// http://issues.apache.org/bugzilla/show_bug.cgi?id=24328
448
public void testPrepareStatementOptions() throws Exception JavaDoc
449     {
450         Connection JavaDoc conn = getConnection();
451         assertTrue(null != conn);
452         PreparedStatement JavaDoc stmt = conn.prepareStatement("select * from dual",
453             ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
454         assertTrue(null != stmt);
455         ResultSet JavaDoc 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     // Bugzilla Bug 24966: NullPointer with Oracle 9 driver
468
// wrong order of passivate/close when a rset isn't closed
469
public void testNoRsetClose() throws Exception JavaDoc {
470         Connection JavaDoc conn = getConnection();
471         assertNotNull(conn);
472         PreparedStatement JavaDoc stmt = conn.prepareStatement("test");
473         assertNotNull(stmt);
474         ResultSet JavaDoc rset = stmt.getResultSet();
475         assertNotNull(rset);
476         // forget to close the resultset: rset.close();
477
stmt.close();
478         conn.close();
479     }
480     
481     // Bugzilla Bug 26966: Connectionpool's connections always returns same
482
public void testHashCode() throws Exception JavaDoc {
483         Connection JavaDoc conn1 = getConnection();
484         assertNotNull(conn1);
485         Connection JavaDoc conn2 = getConnection();
486         assertNotNull(conn2);
487
488         assertTrue(conn1.hashCode() != conn2.hashCode());
489     }
490 }
491
Popular Tags