Author: rmannibucau
Date: Sun Aug 19 18:51:15 2012
New Revision: 1374817

URL: http://svn.apache.org/viewvc?rev=1374817&view=rev
Log:
TOMEE-384 using tccl to get the jdbc driver with tomcat jdbc

Modified:
    
openejb/trunk/openejb/tomee/tomee-jdbc/src/main/java/org/apache/tomee/jdbc/TomEEDataSourceCreator.java

Modified: 
openejb/trunk/openejb/tomee/tomee-jdbc/src/main/java/org/apache/tomee/jdbc/TomEEDataSourceCreator.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/tomee/tomee-jdbc/src/main/java/org/apache/tomee/jdbc/TomEEDataSourceCreator.java?rev=1374817&r1=1374816&r2=1374817&view=diff
==============================================================================
--- 
openejb/trunk/openejb/tomee/tomee-jdbc/src/main/java/org/apache/tomee/jdbc/TomEEDataSourceCreator.java
 (original)
+++ 
openejb/trunk/openejb/tomee/tomee-jdbc/src/main/java/org/apache/tomee/jdbc/TomEEDataSourceCreator.java
 Sun Aug 19 18:51:15 2012
@@ -23,9 +23,11 @@ import org.apache.openejb.monitoring.Obj
 import org.apache.openejb.resource.jdbc.pool.PoolDataSourceCreator;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
+import org.apache.openejb.util.reflection.Reflections;
 import org.apache.tomcat.jdbc.pool.ConnectionPool;
 import org.apache.tomcat.jdbc.pool.PoolConfiguration;
 import org.apache.tomcat.jdbc.pool.PoolProperties;
+import org.apache.tomcat.jdbc.pool.PooledConnection;
 
 import javax.management.ObjectName;
 import javax.sql.DataSource;
@@ -33,6 +35,7 @@ import java.lang.reflect.InvocationHandl
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.sql.Connection;
+import java.sql.Driver;
 import java.sql.SQLException;
 import java.util.Map;
 import java.util.Properties;
@@ -122,6 +125,16 @@ public class TomEEDataSourceCreator exte
             initJmx(name);
         }
 
+        @Override
+        public ConnectionPool createPool() throws SQLException {
+            if (pool != null) {
+                return pool;
+            } else {
+                pool = new TomEEConnectionPool(poolProperties); // to force to 
init the driver with TCCL
+                return pool;
+            }
+        }
+
         public TomEEDataSource(final PoolConfiguration poolConfiguration, 
final String name) {
             super(poolConfiguration);
             try { // just to force the pool to be created and be able to 
register the mbean
@@ -172,6 +185,30 @@ public class TomEEDataSourceCreator exte
         }
     }
 
+    private static class TomEEConnectionPool extends ConnectionPool {
+        public TomEEConnectionPool(final PoolConfiguration poolProperties) 
throws SQLException {
+            super(poolProperties);
+        }
+
+        @Override
+        protected PooledConnection create(boolean incrementCounter) {
+            final PooledConnection con = super.create(incrementCounter);
+            if (getPoolProperties().getDataSource() == null) { // using driver
+                // init driver with TCCL
+                ClassLoader cl = 
Thread.currentThread().getContextClassLoader();
+                if (cl == null) {
+                    cl = TomEEConnectionPool.class.getClassLoader();
+                }
+                try {
+                    Reflections.set(con, "driver", 
Class.forName(getPoolProperties().getDriverClassName(), true, 
cl).newInstance());
+                } catch (java.lang.Exception cn) {
+                    // will fail later, no worry
+                }
+            }
+            return con;
+        }
+    }
+
     private static class ContantHashCodeHandler implements InvocationHandler { 
// will be fixed in tomcat-jdbc in next version
         private final Object delegate;
         private final int hashCode;


Reply via email to