KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbcp > datasources > TestSharedPoolDataSource


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.datasources;
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
24 import javax.sql.DataSource JavaDoc;
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 /**
33  * @author John McNally
34  * @author Dirk Verbeeck
35  * @version $Revision: 1.7 $ $Date: 2004/02/28 11:47:52 $
36  */

37 public class TestSharedPoolDataSource extends TestConnectionPool {
38     public TestSharedPoolDataSource(String JavaDoc testName) {
39         super(testName);
40     }
41
42     public static Test suite() {
43         return new TestSuite(TestSharedPoolDataSource.class);
44     }
45
46     protected Connection JavaDoc getConnection() throws Exception JavaDoc {
47         return ds.getConnection("foo","bar");
48     }
49
50     private DriverAdapterCPDS pcds;
51     private DataSource JavaDoc ds;
52
53     public void setUp() throws Exception JavaDoc {
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     /**
72      * Switching 'u1 -> 'u2' and 'p1' -> 'p2' will
73      * exhibit the bug detailed in
74      * http://nagoya.apache.org/bugzilla/show_bug.cgi?id=18905
75      */

76     public void testIncorrectPassword() throws Exception JavaDoc
77     {
78         try {
79             // Use bad password
80
ds.getConnection("u1", "zlsafjk").close();
81             fail("Able to retrieve connection with incorrect password");
82         } catch (SQLException JavaDoc e1) {
83             // should fail
84

85         }
86         
87         // Use good password
88
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 JavaDoc e)
95         {
96             if (!e.getMessage().startsWith("Given password did not match"))
97             {
98                 throw e;
99             }
100             // else the exception was expected
101
}
102         
103         // Make sure we can still use our good password.
104
ds.getConnection("u1", "p1").close();
105     }
106
107
108     public void testSimple() throws Exception JavaDoc
109     {
110         Connection JavaDoc conn = ds.getConnection();
111         assertTrue(null != conn);
112         PreparedStatement JavaDoc stmt = conn.prepareStatement("select * from dual");
113         assertTrue(null != stmt);
114         ResultSet JavaDoc 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 JavaDoc
123     {
124         Connection JavaDoc conn = ds.getConnection("u1", "p1");
125         assertTrue(null != conn);
126         PreparedStatement JavaDoc stmt = conn.prepareStatement("select * from dual");
127         assertTrue(null != stmt);
128         ResultSet JavaDoc 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 JavaDoc
138     {
139         Connection JavaDoc[] c = new Connection JavaDoc[getMaxActive()];
140         // open the maximum connections
141
for (int i=0; i<c.length; i++)
142         {
143             c[i] = ds.getConnection("u1", "p1");
144         }
145
146         // close one of the connections
147
c[0].close();
148         assertTrue(c[0].isClosed());
149         // get a new connection
150
c[0] = ds.getConnection("u1", "p1");
151
152         for (int i=0; i<c.length; i++)
153         {
154             c[i].close();
155         }
156
157         // open the maximum connections
158
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 JavaDoc
170     {
171         Connection JavaDoc conn = ds.getConnection();
172         assertTrue(null != conn);
173
174         PreparedStatement JavaDoc stmt =
175             conn.prepareStatement("select * from dual");
176         assertTrue(null != stmt);
177         ResultSet JavaDoc 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 JavaDoc e)
198         {
199             // expected
200
}
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 JavaDoc
227     {
228         Connection JavaDoc[] c = new Connection JavaDoc[getMaxActive()];
229         // test that opening new connections is not closing previous
230
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 JavaDoc
248     {
249         Connection JavaDoc[] c = new Connection JavaDoc[getMaxActive()];
250         // open the maximum connections
251
for (int i=0; i<c.length; i++)
252         {
253             c[i] = ds.getConnection();
254         }
255
256         // close one of the connections
257
c[0].close();
258         assertTrue(c[0].isClosed());
259         
260         // get a new connection
261
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 JavaDoc
271     {
272         Connection JavaDoc[] c = new Connection JavaDoc[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 JavaDoc e)
285         {
286             // should only be able to open 10 connections, so this test should
287
// throw an exception
288
}
289
290         for (int i=0; i<c.length; i++)
291         {
292             c[i].close();
293         }
294     }
295     
296     public void testMultipleThreads() throws Exception JavaDoc {
297         assertTrue(multipleThreads(1));
298         assertTrue(!multipleThreads(2 * (int)(getMaxWait())));
299     }
300
301     private boolean multipleThreads(int holdTime) throws Exception JavaDoc {
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 JavaDoc threadGroup = new ThreadGroup JavaDoc("foo") {
307             public void uncaughtException(Thread JavaDoc t, Throwable JavaDoc e) {
308                 /*
309                 for (int i = 0; i < pts.length; i++)
310                 {
311                     System.out.println(i + ": " + pts[i].reportState());
312                 }
313                 */

314                 for (int i = 0; i < pts.length; i++) {
315                     pts[i].stop();
316                 }
317
318                 //e.printStackTrace();
319
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         // - (pts.length*10*holdTime);
332
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 JavaDoc {
341         /**
342          * The number of milliseconds to hold onto a database connection
343          */

344         private int connHoldTime;
345
346         private boolean isRun;
347
348         private String JavaDoc state;
349
350         protected PoolTest(ThreadGroup JavaDoc threadGroup, int connHoldTime) {
351             this.connHoldTime = connHoldTime;
352             Thread JavaDoc thread =
353                 new Thread JavaDoc(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 JavaDoc conn = null;
363                     state = "Getting Connection";
364                     conn = getConnection();
365                     state = "Using Connection";
366                     assertTrue(null != conn);
367                     PreparedStatement JavaDoc stmt =
368                         conn.prepareStatement("select * from dual");
369                     assertTrue(null != stmt);
370                     ResultSet JavaDoc 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 JavaDoc e) {
380                     throw e;
381                 } catch (Exception JavaDoc e) {
382                     throw new RuntimeException JavaDoc(e.toString());
383                 }
384             }
385         }
386
387         public void stop() {
388             isRun = false;
389         }
390
391         public String JavaDoc reportState() {
392             return state;
393         }
394     }
395
396     public void testTransactionIsolationBehavior() throws Exception JavaDoc {
397         Connection JavaDoc 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 JavaDoc conn2 = getConnection();
405         assertEquals(Connection.TRANSACTION_READ_COMMITTED,
406                      conn2.getTransactionIsolation());
407         
408         Connection JavaDoc conn3 = getConnection();
409         assertEquals(Connection.TRANSACTION_READ_COMMITTED,
410                      conn3.getTransactionIsolation());
411         conn2.close();
412         conn3.close();
413     }
414
415
416     // Bugzilla Bug 24136 ClassCastException in DriverAdapterCPDS
417
// when setPoolPreparedStatements(true)
418
public void testPoolPrepareStatement() throws Exception JavaDoc
419     {
420         pcds.setPoolPreparedStatements(true);
421
422         Connection JavaDoc conn = ds.getConnection();
423         assertTrue(null != conn);
424         PreparedStatement JavaDoc stmt = conn.prepareStatement("select * from dual");
425         assertTrue(null != stmt);
426         ResultSet JavaDoc 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