Allow all kinds of data sources to be mapped to the connection pool
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 24 Dec 2009 17:28:20 +0000 (17:28 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 24 Dec 2009 17:28:20 +0000 (17:28 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@893788 13f79535-47bb-0310-9956-ffa450edef68

modules/jdbc-pool/build.properties.default
modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/DataSourceFactory.java
modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/DataSourceProxy.java
modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PoolConfiguration.java
modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PoolProperties.java
modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java
modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/jmx/ConnectionPool.java

index a16f5ce..926b368 100644 (file)
@@ -28,7 +28,7 @@
 version.major=1
 version.minor=0
 version.build=8
-version.patch=.2
+version.patch=.3
 version.suffix=
 
 # ----- Default Base Path for Dependent Packages -----
index 7de726a..fff8327 100644 (file)
@@ -464,10 +464,10 @@ public class DataSourceFactory implements ObjectFactory {
     public static DataSource createDataSource(Properties properties,Context context) throws Exception {
         PoolConfiguration poolProperties = DataSourceFactory.parsePoolProperties(properties);
         if (poolProperties.getDataSourceJNDI()!=null && poolProperties.getDataSource()==null) {
-            javax.sql.DataSource jndiDS = null;
+            javax.sql.CommonDataSource jndiDS = null;
             try {
                 if (context!=null) {
-                    jndiDS = (javax.sql.DataSource)context.lookup(poolProperties.getDataSourceJNDI());
+                    jndiDS = (javax.sql.CommonDataSource)context.lookup(poolProperties.getDataSourceJNDI());
                 } else {
                     log.warn("dataSourceJNDI property is configued, but local JNDI context is null.");
                 }
@@ -477,7 +477,7 @@ public class DataSourceFactory implements ObjectFactory {
             if (jndiDS==null) {
                 try {
                     context = (Context) (new InitialContext());
-                    jndiDS = (javax.sql.DataSource)context.lookup(poolProperties.getDataSourceJNDI());
+                    jndiDS = (javax.sql.CommonDataSource)context.lookup(poolProperties.getDataSourceJNDI());
                 } catch (NamingException e) {
                     log.warn("The name \""+poolProperties.getDataSourceJNDI()+"\" can not be found in the InitialContext.");
                 }
index ad61f38..69a3bcc 100644 (file)
@@ -23,6 +23,7 @@ import java.util.Iterator;
 import java.util.Properties;
 import java.util.concurrent.Future;
 
+import javax.sql.CommonDataSource;
 import javax.sql.XAConnection;
 
 import org.apache.juli.logging.Log;
@@ -1014,14 +1015,14 @@ public class DataSourceProxy implements PoolConfiguration {
     /** 
      * {@inheritDoc}
      */
-    public void setDataSource(javax.sql.DataSource ds) {
+    public void setDataSource(javax.sql.CommonDataSource ds) {
         getPoolProperties().setDataSource(ds);
     }
     
     /** 
      * {@inheritDoc}
      */
-    public javax.sql.DataSource getDataSource() {
+    public CommonDataSource getDataSource() {
         return getPoolProperties().getDataSource();
     }
     
index 0368a6c..3b3cf9e 100644 (file)
@@ -18,6 +18,8 @@ package org.apache.tomcat.jdbc.pool;
 
 import java.util.Properties;
 
+import javax.sql.CommonDataSource;
+
 import org.apache.tomcat.jdbc.pool.PoolProperties.InterceptorDefinition;
 
 /**
@@ -728,17 +730,17 @@ public interface PoolConfiguration {
      * will be invoked.  
      * @param ds the {@link javax.sql.DataSource} to be used for creating connections to be pooled.
      */
-    public void setDataSource(javax.sql.DataSource ds);
+    public void setDataSource(CommonDataSource ds);
     
     /**
      * Returns a datasource, if one exists that is being used to create connections.
      * This method will return null if the pool is using a {@link java.sql.Driver}
      * @return the {@link javax.sql.DataSource} to be used for creating connections to be pooled or null if a Driver is used.
      */
-    public javax.sql.DataSource getDataSource();
+    public CommonDataSource getDataSource();
     
     /**
-     * Configure the connection pool to use a DataSource according to {@link PoolConfiguration#setDataSource(javax.sql.DataSource)}
+     * Configure the connection pool to use a DataSource according to {@link PoolConfiguration#setDataSource(CommonDataSource)}
      * But instead of injecting the object, specify the JNDI location.
      * After a successful JNDI look, the {@link PoolConfiguration#getDataSource()} will not return null. 
      * @param jndiDS -the JNDI string @TODO specify the rules here.
index 66720a1..8e91070 100644 (file)
@@ -25,6 +25,8 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import javax.sql.CommonDataSource;
+
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 
@@ -75,7 +77,7 @@ public class PoolProperties implements PoolConfiguration {
     protected boolean useLock = false;
     protected InterceptorDefinition[] interceptors = null;
     protected int suspectTimeout = 0;
-    protected javax.sql.DataSource dataSource = null;
+    protected javax.sql.CommonDataSource dataSource = null;
     protected String dataSourceJNDI = null;
     
     
@@ -876,14 +878,14 @@ public class PoolProperties implements PoolConfiguration {
     /** 
      * {@inheritDoc}
      */
-    public void setDataSource(javax.sql.DataSource ds) {
+    public void setDataSource(javax.sql.CommonDataSource ds) {
         this.dataSource = ds;
     }
     
     /** 
      * {@inheritDoc}
      */
-    public javax.sql.DataSource getDataSource() {
+    public CommonDataSource getDataSource() {
         return dataSource;
     }
     
index 8f1a31f..2720f35 100644 (file)
@@ -178,13 +178,22 @@ public class PooledConnection {
                 xaConnection = xds.getXAConnection();
                 connection = xaConnection.getConnection();
             }
-        } else {
-            javax.sql.DataSource ds = poolProperties.getDataSource();
+        } else if (poolProperties.getDataSource() instanceof javax.sql.DataSource){
+            javax.sql.DataSource ds = (javax.sql.DataSource)poolProperties.getDataSource();
             if (poolProperties.getUsername()!=null && poolProperties.getPassword()!=null) {
                 connection = ds.getConnection(poolProperties.getUsername(), poolProperties.getPassword());
             } else {
                 connection = ds.getConnection();
             }
+        } else if (poolProperties.getDataSource() instanceof javax.sql.ConnectionPoolDataSource){
+            javax.sql.ConnectionPoolDataSource ds = (javax.sql.ConnectionPoolDataSource)poolProperties.getDataSource();
+            if (poolProperties.getUsername()!=null && poolProperties.getPassword()!=null) {
+                connection = ds.getPooledConnection(poolProperties.getUsername(), poolProperties.getPassword()).getConnection();
+            } else {
+                connection = ds.getPooledConnection().getConnection();
+            }
+        } else {
+            throw new SQLException("DataSource is of unknown class:"+(poolProperties.getDataSource()!=null?poolProperties.getDataSource().getClass():"null"));
         }
     }
     protected void connectUsingDriver() throws SQLException {
index ffbb3b2..9849f20 100644 (file)
@@ -25,6 +25,7 @@ import javax.management.MBeanNotificationInfo;
 import javax.management.Notification;
 import javax.management.NotificationBroadcasterSupport;
 import javax.management.NotificationListener;
+import javax.sql.CommonDataSource;
 
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
@@ -574,14 +575,14 @@ public class ConnectionPool extends NotificationBroadcasterSupport implements Co
    /** 
     * {@inheritDoc}
     */
-   public void setDataSource(javax.sql.DataSource ds) {
+   public void setDataSource(javax.sql.CommonDataSource ds) {
        getPoolProperties().setDataSource(ds);
    }
    
    /** 
     * {@inheritDoc}
     */
-   public javax.sql.DataSource getDataSource() {
+   public CommonDataSource getDataSource() {
        return getPoolProperties().getDataSource();
    }