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:
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027:
028: import org.apache.commons.pool.ObjectPool;
029: import org.apache.commons.pool.impl.GenericObjectPool;
030:
031: /**
032: * @author James Ring
033: * @version $Revision$ $Date$
034: */
035: public class TestPoolableConnection extends TestCase {
036: public TestPoolableConnection(String testName) {
037: super (testName);
038: }
039:
040: public static Test suite() {
041: return new TestSuite(TestPoolableConnection.class);
042: }
043:
044: private ObjectPool pool = null;
045:
046: public void setUp() throws Exception {
047: pool = new GenericObjectPool();
048: PoolableConnectionFactory factory = new PoolableConnectionFactory(
049: new DriverConnectionFactory(new TesterDriver(),
050: "jdbc:apache:commons:testdriver", null), pool,
051: null, null, true, true);
052: pool.setFactory(factory);
053: }
054:
055: public void testConnectionPool() {
056: // Grab a new connection from the pool
057: Connection c = null;
058: try {
059: c = (Connection) pool.borrowObject();
060: } catch (Exception e) {
061: fail("Could not fetch Connection from pool: "
062: + e.getMessage());
063: }
064:
065: assertTrue(
066: "Connection should be created and should not be null",
067: c != null);
068: assertEquals(
069: "There should be exactly one active object in the pool",
070: 1, pool.getNumActive());
071:
072: // Now return the connection by closing it
073: try {
074: c.close();
075: } catch (SQLException e) {
076: fail("Could not close connection: " + e.getMessage());
077: }
078:
079: assertEquals(
080: "There should now be zero active objects in the pool",
081: 0, pool.getNumActive());
082: }
083:
084: // Bugzilla Bug 33591: PoolableConnection leaks connections if the
085: // delegated connection closes itself.
086: public void testPoolableConnectionLeak() {
087: Connection conn = null;
088: try {
089: // 'Borrow' a connection from the pool
090: conn = (Connection) pool.borrowObject();
091:
092: // Now close our innermost delegate, simulating the case where the
093: // underlying connection closes itself
094: ((PoolableConnection) conn).getInnermostDelegate().close();
095:
096: // At this point, we can close the pooled connection. The
097: // PoolableConnection *should* realise that its underlying
098: // connection is gone and invalidate itself. The pool should have no
099: // active connections.
100: } catch (Exception e) {
101: fail("Exception occured while testing connection leak: "
102: + e.getMessage());
103: }
104:
105: try {
106: conn.close();
107: } catch (Exception e) {
108: // Here we expect 'connection already closed', but the connection
109: // should *NOT* be returned to the pool
110: }
111:
112: assertEquals("The pool should have no active connections", 0,
113: pool.getNumActive());
114: }
115: }
|