From c4082ca9fdd5aab2adbe11d51ca432cd82d08e71 Mon Sep 17 00:00:00 2001 From: fhanik Date: Mon, 10 Nov 2008 17:09:40 +0000 Subject: [PATCH] Added test case to test two concurrent datasources, fixed the flag to turn on the sweeper git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@712701 13f79535-47bb-0310-9956-ffa450edef68 --- modules/jdbc-pool/.classpath | 4 +- .../apache/tomcat/jdbc/pool/PoolProperties.java | 2 +- .../apache/tomcat/jdbc/test/DefaultTestCase.java | 9 +++- .../apache/tomcat/jdbc/test/TwoDataSources.java | 53 ++++++++++++++++++++++ 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/TwoDataSources.java diff --git a/modules/jdbc-pool/.classpath b/modules/jdbc-pool/.classpath index 93157c37f..9930b5a99 100644 --- a/modules/jdbc-pool/.classpath +++ b/modules/jdbc-pool/.classpath @@ -3,9 +3,9 @@ - - + + diff --git a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PoolProperties.java b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PoolProperties.java index 1ddbc404f..3a6e2a8df 100644 --- a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PoolProperties.java +++ b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PoolProperties.java @@ -391,7 +391,7 @@ public class PoolProperties { public boolean isPoolSweeperEnabled() { boolean result = getTimeBetweenEvictionRunsMillis()>0; result = result && (isRemoveAbandoned() && getRemoveAbandonedTimeout()>0); - result = result && (isTestWhileIdle() && getValidationQuery()!=null); + result = result || (isTestWhileIdle() && getValidationQuery()!=null); return result; } } diff --git a/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/DefaultTestCase.java b/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/DefaultTestCase.java index 5de13e096..b7377b63b 100644 --- a/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/DefaultTestCase.java +++ b/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/DefaultTestCase.java @@ -39,7 +39,8 @@ public class DefaultTestCase extends TestCase { super(name); } - protected void init() throws Exception { + public DataSourceProxy createDefaultDataSource() { + DataSourceProxy datasource = null; PoolProperties p = new DefaultProperties(); p.setJmxEnabled(false); p.setTestWhileIdle(false); @@ -57,6 +58,11 @@ public class DefaultTestCase extends TestCase { p.setRemoveAbandoned(false); datasource = new org.apache.tomcat.jdbc.pool.DataSourceProxy(); datasource.setPoolProperties(p); + return datasource; + } + + protected void init() throws Exception { + this.datasource = createDefaultDataSource(); } protected void transferProperties() { @@ -92,6 +98,7 @@ public class DefaultTestCase extends TestCase { protected void tearDown() throws Exception { + try {datasource.close();}catch(Exception ignore){} datasource = null; tDatasource = null; System.gc(); diff --git a/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/TwoDataSources.java b/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/TwoDataSources.java new file mode 100644 index 000000000..37a6f3880 --- /dev/null +++ b/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/TwoDataSources.java @@ -0,0 +1,53 @@ +package org.apache.tomcat.jdbc.test; + +import java.sql.Connection; + +import org.apache.tomcat.jdbc.pool.DataSourceProxy; + +public class TwoDataSources extends DefaultTestCase { + + public TwoDataSources(String name) { + super(name); + } + + public void testTwoDataSources() throws Exception { + DataSourceProxy d1 = this.createDefaultDataSource(); + DataSourceProxy d2 = this.createDefaultDataSource(); + d1.setRemoveAbandoned(true); + d1.setRemoveAbandonedTimeout(10); + d1.setTimeBetweenEvictionRunsMillis(1000); + d2.setRemoveAbandoned(false); + Connection c1 = d1.getConnection(); + Connection c2 = d2.getConnection(); + Thread.sleep(15000); + try { + c1.createStatement(); + this.assertTrue("Connection should have been abandoned.",false); + }catch (Exception x) { + this.assertTrue("This is correct, c1 is abandoned",true); + } + + try { + c2.createStatement(); + this.assertTrue("Connection should not have been abandoned.",true); + }catch (Exception x) { + this.assertTrue("Connection c2 should be working",false); + } + try { + c1.close(); + this.assertTrue("Connection should have been closed.",false); + }catch (Exception x) { + this.assertTrue("This is correct, c1 is closed",true); + } + try { + c2.close(); + this.assertTrue("Connection c2 should not have been closed.",true); + }catch (Exception x) { + this.assertTrue("Connection c2 should be working",false); + } + + + + } + +} -- 2.11.0