001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.dbcp;
019:
020: import java.sql.Connection;
021: import java.sql.PreparedStatement;
022: import java.sql.SQLException;
023: import java.sql.Statement;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027:
028: /**
029: * TestSuite for BasicDataSource with prepared statement pooling enabled
030: *
031: * @author Dirk Verbeeck
032: * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
033: */
034: public class TestPStmtPoolingBasicDataSource extends
035: TestBasicDataSource {
036: public TestPStmtPoolingBasicDataSource(String testName) {
037: super (testName);
038: }
039:
040: public static Test suite() {
041: return new TestSuite(TestPStmtPoolingBasicDataSource.class);
042: }
043:
044: public void setUp() throws Exception {
045: super .setUp();
046:
047: // PoolPreparedStatements enabled, should not affect the basic tests
048: ds.setPoolPreparedStatements(true);
049: ds.setMaxOpenPreparedStatements(2);
050: }
051:
052: public void tearDown() throws Exception {
053: super .tearDown();
054: // nothing to do here
055: }
056:
057: public void testPreparedStatementPooling() throws Exception {
058: Connection conn = getConnection();
059: assertNotNull(conn);
060:
061: PreparedStatement stmt1 = conn
062: .prepareStatement("select 'a' from dual");
063: assertNotNull(stmt1);
064:
065: PreparedStatement stmt2 = conn
066: .prepareStatement("select 'b' from dual");
067: assertNotNull(stmt2);
068:
069: assertTrue(stmt1 != stmt2);
070:
071: // go over the maxOpen limit
072: PreparedStatement stmt3 = null;
073: try {
074: stmt3 = conn.prepareStatement("select 'c' from dual");
075: fail("expected SQLException");
076: } catch (SQLException e) {
077: }
078:
079: // make idle
080: stmt2.close();
081:
082: // test cleanup the 'b' statement
083: stmt3 = conn.prepareStatement("select 'c' from dual");
084: assertNotNull(stmt3);
085: assertTrue(stmt3 != stmt1);
086: assertTrue(stmt3 != stmt2);
087:
088: // normal reuse of statement
089: stmt1.close();
090: PreparedStatement stmt4 = conn
091: .prepareStatement("select 'a' from dual");
092: assertNotNull(stmt4);
093: }
094:
095: // Bugzilla Bug 27246
096: // PreparedStatement cache should be different depending on the Catalog
097: public void testPStmtCatalog() throws Exception {
098: Connection conn = getConnection();
099: conn.setCatalog("catalog1");
100: DelegatingPreparedStatement stmt1 = (DelegatingPreparedStatement) conn
101: .prepareStatement("select 'a' from dual");
102: TesterPreparedStatement inner1 = (TesterPreparedStatement) stmt1
103: .getInnermostDelegate();
104: assertEquals("catalog1", inner1.getCatalog());
105: stmt1.close();
106:
107: conn.setCatalog("catalog2");
108: DelegatingPreparedStatement stmt2 = (DelegatingPreparedStatement) conn
109: .prepareStatement("select 'a' from dual");
110: TesterPreparedStatement inner2 = (TesterPreparedStatement) stmt2
111: .getInnermostDelegate();
112: assertEquals("catalog2", inner2.getCatalog());
113: stmt2.close();
114:
115: conn.setCatalog("catalog1");
116: DelegatingPreparedStatement stmt3 = (DelegatingPreparedStatement) conn
117: .prepareStatement("select 'a' from dual");
118: TesterPreparedStatement inner3 = (TesterPreparedStatement) stmt1
119: .getInnermostDelegate();
120: assertEquals("catalog1", inner3.getCatalog());
121: stmt3.close();
122:
123: assertNotSame(inner1, inner2);
124: assertSame(inner1, inner3);
125: }
126:
127: public void testPStmtPoolingWithNoClose() throws Exception {
128: ds.setMaxActive(1); // only one connection in pool needed
129: ds.setMaxIdle(1);
130: ds.setAccessToUnderlyingConnectionAllowed(true);
131: Connection conn1 = getConnection();
132: assertNotNull(conn1);
133: assertEquals(1, ds.getNumActive());
134: assertEquals(0, ds.getNumIdle());
135:
136: PreparedStatement stmt1 = conn1
137: .prepareStatement("select 'a' from dual");
138: assertNotNull(stmt1);
139:
140: Statement inner1 = ((DelegatingPreparedStatement) stmt1)
141: .getInnermostDelegate();
142: assertNotNull(inner1);
143:
144: stmt1.close();
145:
146: Connection conn2 = conn1;
147: assertNotNull(conn2);
148: assertEquals(1, ds.getNumActive());
149: assertEquals(0, ds.getNumIdle());
150:
151: PreparedStatement stmt2 = conn2
152: .prepareStatement("select 'a' from dual");
153: assertNotNull(stmt2);
154:
155: Statement inner2 = ((DelegatingPreparedStatement) stmt2)
156: .getInnermostDelegate();
157: assertNotNull(inner2);
158:
159: assertSame(inner1, inner2);
160: }
161:
162: public void testPStmtPoolingAccrossClose() throws Exception {
163: ds.setMaxActive(1); // only one connection in pool needed
164: ds.setMaxIdle(1);
165: ds.setAccessToUnderlyingConnectionAllowed(true);
166: Connection conn1 = getConnection();
167: assertNotNull(conn1);
168: assertEquals(1, ds.getNumActive());
169: assertEquals(0, ds.getNumIdle());
170:
171: PreparedStatement stmt1 = conn1
172: .prepareStatement("select 'a' from dual");
173: assertNotNull(stmt1);
174:
175: Statement inner1 = ((DelegatingPreparedStatement) stmt1)
176: .getInnermostDelegate();
177: assertNotNull(inner1);
178:
179: stmt1.close();
180: conn1.close();
181:
182: assertEquals(0, ds.getNumActive());
183: assertEquals(1, ds.getNumIdle());
184:
185: Connection conn2 = getConnection();
186: assertNotNull(conn2);
187: assertEquals(1, ds.getNumActive());
188: assertEquals(0, ds.getNumIdle());
189:
190: PreparedStatement stmt2 = conn2
191: .prepareStatement("select 'a' from dual");
192: assertNotNull(stmt2);
193:
194: Statement inner2 = ((DelegatingPreparedStatement) stmt2)
195: .getInnermostDelegate();
196: assertNotNull(inner2);
197:
198: assertSame(inner1, inner2);
199: }
200: }
|