geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From xuhaih...@apache.org
Subject svn commit: r1136268 - in /geronimo/server/trunk/framework/modules/geronimo-jdbc: pom.xml src/main/java/org/apache/geronimo/jdbc/DatabaseDriverExtender.java src/main/java/org/apache/geronimo/jdbc/JdbcLeakPreventionListener.java
Date Thu, 16 Jun 2011 01:57:13 GMT
Author: xuhaihong
Date: Thu Jun 16 01:57:12 2011
New Revision: 1136268

URL: http://svn.apache.org/viewvc?rev=1136268&view=rev
Log:
GERONIMO-6009 Add a JDBC driver leak prevention bundle listener

Added:
    geronimo/server/trunk/framework/modules/geronimo-jdbc/src/main/java/org/apache/geronimo/jdbc/JdbcLeakPreventionListener.java
Modified:
    geronimo/server/trunk/framework/modules/geronimo-jdbc/pom.xml
    geronimo/server/trunk/framework/modules/geronimo-jdbc/src/main/java/org/apache/geronimo/jdbc/DatabaseDriverExtender.java

Modified: geronimo/server/trunk/framework/modules/geronimo-jdbc/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-jdbc/pom.xml?rev=1136268&r1=1136267&r2=1136268&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-jdbc/pom.xml (original)
+++ geronimo/server/trunk/framework/modules/geronimo-jdbc/pom.xml Thu Jun 16 01:57:12 2011
@@ -39,6 +39,10 @@
             <groupId>org.ops4j.pax.logging</groupId>
             <artifactId>pax-logging-api</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.xbean</groupId>
+            <artifactId>xbean-bundleutils</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

Modified: geronimo/server/trunk/framework/modules/geronimo-jdbc/src/main/java/org/apache/geronimo/jdbc/DatabaseDriverExtender.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-jdbc/src/main/java/org/apache/geronimo/jdbc/DatabaseDriverExtender.java?rev=1136268&r1=1136267&r2=1136268&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-jdbc/src/main/java/org/apache/geronimo/jdbc/DatabaseDriverExtender.java
(original)
+++ geronimo/server/trunk/framework/modules/geronimo-jdbc/src/main/java/org/apache/geronimo/jdbc/DatabaseDriverExtender.java
Thu Jun 16 01:57:12 2011
@@ -42,21 +42,29 @@ import org.slf4j.LoggerFactory;
 public class DatabaseDriverExtender implements BundleActivator {
 
     private static final Logger LOG = LoggerFactory.getLogger(DatabaseDriverExtender.class);
-    
+
     private BundleTracker tracker;
-    
+
+    private JdbcLeakPreventionListener jdbcPreventionListener;
+
     public void start(BundleContext context) throws Exception {
         tracker = new BundleTracker(context, Bundle.ACTIVE, new DriverBundleTrackerCustomizer());
         tracker.open();
+
+        jdbcPreventionListener = new JdbcLeakPreventionListener();
+        context.addBundleListener(jdbcPreventionListener);
     }
 
     public void stop(BundleContext context) throws Exception {
-        tracker.close();        
+        tracker.close();
+        context.removeBundleListener(jdbcPreventionListener);
+        //Remove any driver from myself
+        JdbcLeakPreventionListener.unRegisterJdbcDrivers(context.getBundle());
     }
 
     private static List<Driver> loadDrivers(Bundle bundle, URL providerURL) {
         List<Driver> drivers = new ArrayList<Driver>();
-        
+
         try {
             BufferedReader in = new BufferedReader(new InputStreamReader(providerURL.openStream()));
             String line;
@@ -64,33 +72,33 @@ public class DatabaseDriverExtender impl
                 line = line.trim();
 
                 try {
-                    Class driverClass = bundle.loadClass(line);
+                    Class<?> driverClass = bundle.loadClass(line);
                     drivers.add( (Driver) driverClass.newInstance());
                 } catch (Exception e) {
                     LOG.warn("Failed to load driver {}", line, e);
                 }
-                
+
             }
         } catch (IOException e) {
             // ignore - shouldn't happen
             LOG.warn("Error reading {} service file", providerURL);
         }
-        
+
         return drivers;
     }
-    
+
     private static void register(List<Driver> drivers) {
         for (Driver driver : drivers) {
             DelegatingDriver.registerDriver(driver);
         }
     }
-    
+
     private static void unregister(List<Driver> drivers) {
         for (Driver driver : drivers) {
             DelegatingDriver.unregisterDriver(driver);
         }
     }
-    
+
     private static class DriverBundleTrackerCustomizer implements BundleTrackerCustomizer
{
 
         public Object addingBundle(Bundle bundle, BundleEvent event) {
@@ -112,7 +120,7 @@ public class DatabaseDriverExtender impl
             unregister(drivers);
             LOG.debug("Unregistered {} drivers in bundle {}", drivers, bundle);
         }
-        
+
     }
-    
+
 }

Added: geronimo/server/trunk/framework/modules/geronimo-jdbc/src/main/java/org/apache/geronimo/jdbc/JdbcLeakPreventionListener.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-jdbc/src/main/java/org/apache/geronimo/jdbc/JdbcLeakPreventionListener.java?rev=1136268&view=auto
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-jdbc/src/main/java/org/apache/geronimo/jdbc/JdbcLeakPreventionListener.java
(added)
+++ geronimo/server/trunk/framework/modules/geronimo-jdbc/src/main/java/org/apache/geronimo/jdbc/JdbcLeakPreventionListener.java
Thu Jun 16 01:57:12 2011
@@ -0,0 +1,72 @@
+/**
+ *  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.geronimo.jdbc;
+
+import java.sql.Driver;
+import java.sql.DriverManager;
+import java.util.Enumeration;
+
+import org.apache.xbean.osgi.bundle.util.BundleUtils;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @version $Rev$ $Date$
+ */
+
+public class JdbcLeakPreventionListener implements BundleListener {
+
+    private static final Logger logger = LoggerFactory.getLogger(JdbcLeakPreventionListener.class);
+
+    @Override
+    public void bundleChanged(BundleEvent event) {
+        if (event.getType() == BundleEvent.UNINSTALLED) {
+            Bundle bundle = event.getBundle();
+            unRegisterJdbcDrivers(bundle);
+        }
+    }
+
+    public static void unRegisterJdbcDrivers(Bundle bundle) {
+        /*
+         * DriverManager.getDrivers() has a nasty side-effect of registering
+         * drivers that are visible to this class loader but haven't yet been
+         * loaded. Therefore, the first call to this method a) gets the list
+         * of originally loaded drivers and b) triggers the unwanted
+         * side-effect. The second call gets the complete list of drivers
+         * ensuring that both original drivers and any loaded as a result of the
+         * side-effects are all de-registered.
+         */
+        Enumeration<Driver> drivers = DriverManager.getDrivers();
+        drivers = DriverManager.getDrivers();
+        while (drivers.hasMoreElements()) {
+            Driver driver = drivers.nextElement();
+            Bundle driverBundle = BundleUtils.getBundle(driver.getClass().getClassLoader(),
true);
+            if (driverBundle != bundle) {
+                continue;
+            }
+            try {
+                DriverManager.deregisterDriver(driver);
+            } catch (Exception e) {
+                logger.warn("Fail to unregister the driver " + driver.getClass().getName(),
e);
+            }
+        }
+    }
+}



Mime
View raw message