KickJava   Java API By Example, From Geeks To Geeks.

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


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.SQLException JavaDoc;
22 import java.sql.Statement JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 /**
28  * TestSuite for BasicDataSource with prepared statement pooling enabled
29  *
30  * @author Dirk Verbeeck
31  * @version $Revision: 1.5 $ $Date: 2004/03/07 15:29:06 $
32  */

33 public class TestPStmtPoolingBasicDataSource extends TestBasicDataSource {
34     public TestPStmtPoolingBasicDataSource(String JavaDoc testName) {
35         super(testName);
36     }
37
38     public static Test suite() {
39         return new TestSuite(TestPStmtPoolingBasicDataSource.class);
40     }
41
42     public void setUp() throws Exception JavaDoc {
43         super.setUp();
44
45         // PoolPreparedStatements enabled, should not affect the basic tests
46
ds.setPoolPreparedStatements(true);
47         ds.setMaxOpenPreparedStatements(2);
48     }
49
50     public void tearDown() throws Exception JavaDoc {
51         super.tearDown();
52         // nothing to do here
53
}
54
55     public void testPreparedStatementPooling() throws Exception JavaDoc {
56         Connection JavaDoc conn = getConnection();
57         assertNotNull(conn);
58         
59         PreparedStatement JavaDoc stmt1 = conn.prepareStatement("select 'a' from dual");
60         assertNotNull(stmt1);
61         
62         PreparedStatement JavaDoc stmt2 = conn.prepareStatement("select 'b' from dual");
63         assertNotNull(stmt2);
64         
65         assertTrue(stmt1 != stmt2);
66         
67         // go over the maxOpen limit
68
PreparedStatement JavaDoc stmt3 = null;
69         try {
70             stmt3 = conn.prepareStatement("select 'c' from dual");
71             fail("expected SQLException");
72         }
73         catch (SQLException JavaDoc e) {}
74         
75         // make idle
76
stmt2.close();
77
78         // test cleanup the 'b' statement
79
stmt3 = conn.prepareStatement("select 'c' from dual");
80         assertNotNull(stmt3);
81         assertTrue(stmt3 != stmt1);
82         assertTrue(stmt3 != stmt2);
83         
84         // normal reuse of statement
85
stmt1.close();
86         PreparedStatement JavaDoc stmt4 = conn.prepareStatement("select 'a' from dual");
87         assertNotNull(stmt4);
88     }
89
90     // Bugzilla Bug 27246
91
// PreparedStatement cache should be different depending on the Catalog
92
public void testPStmtCatalog() throws Exception JavaDoc {
93         Connection JavaDoc conn = getConnection();
94         conn.setCatalog("catalog1");
95         DelegatingPreparedStatement stmt1 = (DelegatingPreparedStatement) conn.prepareStatement("select 'a' from dual");
96         TesterPreparedStatement inner1 = (TesterPreparedStatement) stmt1.getInnermostDelegate();
97         assertEquals("catalog1", inner1.getCatalog());
98         stmt1.close();
99         
100         conn.setCatalog("catalog2");
101         DelegatingPreparedStatement stmt2 = (DelegatingPreparedStatement) conn.prepareStatement("select 'a' from dual");
102         TesterPreparedStatement inner2 = (TesterPreparedStatement) stmt2.getInnermostDelegate();
103         assertEquals("catalog2", inner2.getCatalog());
104         stmt2.close();
105
106         conn.setCatalog("catalog1");
107         DelegatingPreparedStatement stmt3 = (DelegatingPreparedStatement) conn.prepareStatement("select 'a' from dual");
108         TesterPreparedStatement inner3 = (TesterPreparedStatement) stmt1.getInnermostDelegate();
109         assertEquals("catalog1", inner3.getCatalog());
110         stmt3.close();
111         
112         assertNotSame(inner1, inner2);
113         assertSame(inner1, inner3);
114     }
115
116     public void testPStmtPoolingWithNoClose() throws Exception JavaDoc {
117         ds.setMaxActive(1); // only one connection in pool needed
118
ds.setMaxIdle(1);
119         ds.setAccessToUnderlyingConnectionAllowed(true);
120         Connection JavaDoc conn1 = getConnection();
121         assertNotNull(conn1);
122         assertEquals(1, ds.getNumActive());
123         assertEquals(0, ds.getNumIdle());
124         
125         PreparedStatement JavaDoc stmt1 = conn1.prepareStatement("select 'a' from dual");
126         assertNotNull(stmt1);
127         
128         Statement JavaDoc inner1 = ((DelegatingPreparedStatement) stmt1).getInnermostDelegate();
129         assertNotNull(inner1);
130         
131         stmt1.close();
132
133         Connection JavaDoc conn2 = conn1;
134         assertNotNull(conn2);
135         assertEquals(1, ds.getNumActive());
136         assertEquals(0, ds.getNumIdle());
137         
138         PreparedStatement JavaDoc stmt2 = conn2.prepareStatement("select 'a' from dual");
139         assertNotNull(stmt2);
140
141         Statement JavaDoc inner2 = ((DelegatingPreparedStatement) stmt2).getInnermostDelegate();
142         assertNotNull(inner2);
143         
144         assertSame(inner1, inner2);
145     }
146     
147     public void testPStmtPoolingAccrossClose() throws Exception JavaDoc {
148         ds.setMaxActive(1); // only one connection in pool needed
149
ds.setMaxIdle(1);
150         ds.setAccessToUnderlyingConnectionAllowed(true);
151         Connection JavaDoc conn1 = getConnection();
152         assertNotNull(conn1);
153         assertEquals(1, ds.getNumActive());
154         assertEquals(0, ds.getNumIdle());
155         
156         PreparedStatement JavaDoc stmt1 = conn1.prepareStatement("select 'a' from dual");
157         assertNotNull(stmt1);
158         
159         Statement JavaDoc inner1 = ((DelegatingPreparedStatement) stmt1).getInnermostDelegate();
160         assertNotNull(inner1);
161         
162         stmt1.close();
163         conn1.close();
164
165         assertEquals(0, ds.getNumActive());
166         assertEquals(1, ds.getNumIdle());
167
168         Connection JavaDoc conn2 = getConnection();
169         assertNotNull(conn2);
170         assertEquals(1, ds.getNumActive());
171         assertEquals(0, ds.getNumIdle());
172         
173         PreparedStatement JavaDoc stmt2 = conn2.prepareStatement("select 'a' from dual");
174         assertNotNull(stmt2);
175
176         Statement JavaDoc inner2 = ((DelegatingPreparedStatement) stmt2).getInnermostDelegate();
177         assertNotNull(inner2);
178         
179         assertSame(inner1, inner2);
180     }
181 }
182
Popular Tags