From 6d106bb2d7a2bfc02b1f99a82f23b3479650ef74 Mon Sep 17 00:00:00 2001 From: fhanik Date: Thu, 6 Jan 2011 23:53:11 +0000 Subject: [PATCH] Fix bug https://issues.apache.org/bugzilla/show_bug.cgi?id=50477 git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1056125 13f79535-47bb-0310-9956-ffa450edef68 --- modules/jdbc-pool/.classpath | 1 + .../apache/tomcat/jdbc/pool/ConnectionPool.java | 18 ++++++-- .../tomcat/jdbc/test/Async0IdleTestBug50477.java | 50 ++++++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/Async0IdleTestBug50477.java diff --git a/modules/jdbc-pool/.classpath b/modules/jdbc-pool/.classpath index 70a0c729d..2a0361ba0 100644 --- a/modules/jdbc-pool/.classpath +++ b/modules/jdbc-pool/.classpath @@ -6,5 +6,6 @@ + diff --git a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java index db63722d4..63bf0b725 100644 --- a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java +++ b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java @@ -140,6 +140,10 @@ public class ConnectionPool { * @throws SQLException */ public Future getConnectionAsync() throws SQLException { + PooledConnection pc = this.borrowConnection(0, null, null); + if (pc!=null) { + return new ConnectionFuture(pc); + } //we can only retrieve a future if the underlying queue supports it. if (idle instanceof FairBlockingQueue) { Future pcf = ((FairBlockingQueue)idle).pollAsync(); @@ -1040,15 +1044,21 @@ public class ConnectionPool { Connection result = null; SQLException cause = null; AtomicBoolean cancelled = new AtomicBoolean(false); + volatile PooledConnection pc = null; public ConnectionFuture(Future pcf) { this.pcFuture = pcf; } + public ConnectionFuture(PooledConnection pc) { + this.pc = pc; + } /** * {@inheritDoc} */ public boolean cancel(boolean mayInterruptIfRunning) { - if ((!cancelled.get()) && cancelled.compareAndSet(false, true)) { + if (pc!=null) { + return false; + } else if ((!cancelled.get()) && cancelled.compareAndSet(false, true)) { //cancel by retrieving the connection and returning it to the pool ConnectionPool.this.cancellator.execute(this); } @@ -1070,7 +1080,7 @@ public class ConnectionPool { * {@inheritDoc} */ public Connection get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { - PooledConnection pc = pcFuture.get(timeout,unit); + PooledConnection pc = this.pc!=null?this.pc:pcFuture.get(timeout,unit); if (pc!=null) { if (result!=null) return result; if (configured.compareAndSet(false, true)) { @@ -1097,14 +1107,14 @@ public class ConnectionPool { * {@inheritDoc} */ public boolean isCancelled() { - return pcFuture.isCancelled() || cancelled.get(); + return pc==null && (pcFuture.isCancelled() || cancelled.get()); } /** * {@inheritDoc} */ public boolean isDone() { - return pcFuture.isDone(); + return pc!=null || pcFuture.isDone(); } /** diff --git a/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/Async0IdleTestBug50477.java b/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/Async0IdleTestBug50477.java new file mode 100644 index 000000000..cebc458be --- /dev/null +++ b/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/Async0IdleTestBug50477.java @@ -0,0 +1,50 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tomcat.jdbc.test; + +import java.sql.Connection; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.apache.tomcat.jdbc.pool.DataSourceProxy; + +/** + * @author Filip Hanik + * @version 1.0 + */ +public class Async0IdleTestBug50477 extends DefaultTestCase { + public Async0IdleTestBug50477(String name) { + super(name); + } + + + public void testAsync0Idle0Size() throws Exception { + System.out.println("[testPoolThreads20Connections10FairAsync] Starting fairness - Tomcat JDBC - Fair - Async"); + init(); + this.datasource.getPoolProperties().setMaxActive(10); + this.datasource.getPoolProperties().setFairQueue(true); + this.datasource.getPoolProperties().setInitialSize(0); + try { + Future cf = ((DataSourceProxy)datasource).getConnectionAsync(); + Connection con = cf.get(5, TimeUnit.SECONDS); + }finally { + tearDown(); + } + } +} + -- 2.11.0