db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lily...@apache.org
Subject svn commit: r1063996 - in /db/derby/code/trunk/java: engine/org/apache/derby/jdbc/AutoloadedDriver.java engine/org/apache/derby/jdbc/EmbeddedDataSource.java testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/AutoloadTest.java
Date Thu, 27 Jan 2011 05:08:01 GMT
Author: lilywei
Date: Thu Jan 27 05:08:01 2011
New Revision: 1063996

URL: http://svn.apache.org/viewvc?rev=1063996&view=rev
Log:
DERBY-2905 Shutting down embedded Derby will remove AutoloadedDriver. The AutoloadedDriver
is not left registered in the DriverManager.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/jdbc/AutoloadedDriver.java
    db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbeddedDataSource.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/AutoloadTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/jdbc/AutoloadedDriver.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/jdbc/AutoloadedDriver.java?rev=1063996&r1=1063995&r2=1063996&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/jdbc/AutoloadedDriver.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/jdbc/AutoloadedDriver.java Thu Jan 27
05:08:01 2011
@@ -34,6 +34,7 @@ import org.apache.derby.iapi.reference.M
 import org.apache.derby.iapi.reference.Attribute;
 import org.apache.derby.iapi.services.i18n.MessageService;
 import org.apache.derby.iapi.jdbc.JDBCBoot;
+import org.apache.derby.shared.common.sanity.SanityManager;
 
 
 /**
@@ -59,6 +60,12 @@ public class AutoloadedDriver implements
 	// This flag is set if the engine is forcibly brought down.
 	private	static	boolean	_engineForcedDown = false;
 	
+    // This flag is set if AutoloadedDriver exists
+    private static boolean activeautoloadeddriver = false;
+
+    //This is the driver that memorizes the autoloadeddriver (DERBY-2905)
+    private static Driver _autoloadedDriver;
+
 	//
 	// This is the driver that's specific to the JDBC level we're running at.
 	// It's the module which boots the whole Derby engine.
@@ -68,7 +75,9 @@ public class AutoloadedDriver implements
 	static
 	{
 		try {
-			DriverManager.registerDriver( new AutoloadedDriver() );
+            _autoloadedDriver = new AutoloadedDriver();
+            DriverManager.registerDriver( _autoloadedDriver );
+            activeautoloadeddriver = true;
 		}
 		catch (SQLException se)
 		{
@@ -180,7 +189,7 @@ public class AutoloadedDriver implements
 	*/
 	static	Driver getDriverModule() throws SQLException {
 
-		if ( _engineForcedDown )
+		if ( _engineForcedDown && (_autoloadedDriver == null))
 		{
 			// Driver not registered 
 			throw new SQLException
@@ -192,23 +201,45 @@ public class AutoloadedDriver implements
 		return _driverModule;
 	}
 	
-	/*
+	/**
 	** Record which driver module actually booted.
-	*/
+	*  @param driver the driver register to DriverManager is not AutoloadedDriver
+	**/
 	static	void	registerDriverModule( Driver driver )
 	{
 		_driverModule = driver;
 		_engineForcedDown = false;
+		
+        try {
+            if (!activeautoloadeddriver)
+                DriverManager.registerDriver(_driverModule);
+        } catch (SQLException e) {
+            if (SanityManager.DEBUG)
+                SanityManager.THROWASSERT(e);
+        }
 	}
 	
-	/*
-	** Unregister the driver. This happens when the engine is
-	** forcibly shut down.
-	*/
+	/**
+	** Unregister the driver and the AutoloadedDriver if exists. 
+	*  This happens when the engine is forcibly shut down.
+	*  
+	**/
 	static	void	unregisterDriverModule()
 	{
-		_driverModule = null;
 		_engineForcedDown = true;
+        try {
+            if (activeautoloadeddriver) {
+                DriverManager.deregisterDriver(_autoloadedDriver);
+                activeautoloadeddriver = false;
+                _autoloadedDriver = null;
+            } else {
+                DriverManager.deregisterDriver(_driverModule);
+            }
+            _driverModule = null;
+        } catch (SQLException e) {
+            if (SanityManager.DEBUG)
+                SanityManager.THROWASSERT(e);
+        }
 	}
 	
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbeddedDataSource.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbeddedDataSource.java?rev=1063996&r1=1063995&r2=1063996&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbeddedDataSource.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbeddedDataSource.java Thu Jan
27 05:08:01 2011
@@ -24,6 +24,7 @@ package org.apache.derby.jdbc;
 import org.apache.derby.iapi.reference.Attribute;
 
 import java.sql.Connection;
+import java.sql.Driver;
 import java.sql.DriverManager;
 import java.sql.SQLException;
 
@@ -501,11 +502,16 @@ public class EmbeddedDataSource extends 
 
 				// If we know the driver, we loaded it.   Otherwise only
 				// work if DriverManager has already loaded it.
-
-				AutoloadedDriver	autoloadedDriver =
-					(AutoloadedDriver) DriverManager.getDriver(url);
-				driver = (InternalDriver) autoloadedDriver.getDriverModule();
-				// DriverManager will throw an exception if it cannot find the driver
+                // DriverManager will throw an exception if driver is not found
+                Driver registerDriver = DriverManager.getDriver(url);
+                if (registerDriver instanceof AutoloadedDriver) {
+                    AutoloadedDriver autoloadedDriver = 
+                        (AutoloadedDriver) registerDriver;
+                    driver = (InternalDriver) autoloadedDriver
+                            .getDriverModule();
+                } else {
+                    driver = (InternalDriver) registerDriver;
+                }
 			}
 		}
 		return driver;

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/AutoloadTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/AutoloadTest.java?rev=1063996&r1=1063995&r2=1063996&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/AutoloadTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/AutoloadTest.java
Thu Jan 27 05:08:01 2011
@@ -22,8 +22,10 @@
 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
 
 import java.sql.Driver;
+import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.SQLException;
+import javax.sql.DataSource;
 import java.util.Enumeration;
 
 import junit.framework.Test;
@@ -33,6 +35,7 @@ import org.apache.derby.drda.NetworkServ
 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
 import org.apache.derbyTesting.junit.Derby;
 import org.apache.derbyTesting.junit.JDBC;
+import org.apache.derbyTesting.junit.JDBCDataSource;
 import org.apache.derbyTesting.junit.NetworkServerTestSetup;
 import org.apache.derbyTesting.junit.SecurityManagerSetup;
 import org.apache.derbyTesting.junit.TestConfiguration;
@@ -157,7 +160,7 @@ public class AutoloadTest extends BaseJD
         }
         
         suite.addTest(new AutoloadTest("testSuccessfulConnect"));
-        
+      	
         if ("embedded".equals(which)) {
             suite.addTest(SecurityManagerSetup.noSecurityManager(
                 new AutoloadTest("testEmbeddedStarted")));
@@ -165,6 +168,10 @@ public class AutoloadTest extends BaseJD
 
         suite.addTest(new AutoloadTest("testUnsuccessfulConnect"));
         suite.addTest(new AutoloadTest("testExplicitLoad"));
+
+	 if ("embedded".equals(which)) {
+            suite.addTest(new AutoloadTest("testAutoloadDriverUnregister"));
+        }
         return suite;
     }
 
@@ -173,6 +180,54 @@ public class AutoloadTest extends BaseJD
 	// TEST ENTRY POINTS
 	//
 	// ///////////////////////////////////////////////////////////
+
+    /**
+     * Test DERBY-2905:Shutting down embedded Derby does not remove all code,
+     * the AutoloadDriver is left registered in the DriverManager.
+     * 
+     * @throws Exception
+     */
+    public void testAutoloadDriverUnregister() throws Exception {
+        if (usingEmbedded()) {
+            String AutoloadedDriver = "org.apache.derby.jdbc.AutoloadedDriver";
+            String Driver40 = "org.apache.derby.jdbc.Driver40";
+            String Driver30 = "org.apache.derby.jdbc.Driver30";
+            String Driver20 = "org.apache.derby.jdbc.Driver20";
+
+            // Test whether the Autoload driver successfully unregister after
+            // DB shutdown.
+            String url = getTestConfiguration().getJDBCUrl();
+            url = url.concat(";create=true");
+            String user = getTestConfiguration().getUserName();
+            String password = getTestConfiguration().getUserPassword();
+            DriverManager.getConnection(url, user, password);
+
+            assertTrue(getRegisteredDrivers(AutoloadedDriver));
+
+            // shut down engine
+            TestConfiguration.getCurrent().shutdownEngine();
+
+            assertFalse(getRegisteredDrivers(AutoloadedDriver));
+
+            // Test explicit loading of Embedded driver after Autoload driver
+            // is un-registered.
+            String driverClass = getTestConfiguration().getJDBCClient()
+                    .getJDBCDriverName();
+
+            Class.forName(driverClass).newInstance();
+            url = getTestConfiguration().getJDBCUrl();
+            user = getTestConfiguration().getUserName();
+            password = getTestConfiguration().getUserPassword();
+            DriverManager.getConnection(url, user, password);
+
+            // shut down engine
+            TestConfiguration.getCurrent().shutdownEngine();
+
+            assertFalse(getRegisteredDrivers(Driver40));
+            assertFalse(getRegisteredDrivers(Driver30));
+            assertFalse(getRegisteredDrivers(Driver20));
+        }
+    }
     
     /**
      * @throws SQLException
@@ -197,8 +252,8 @@ public class AutoloadTest extends BaseJD
         assertTrue(isEmbeddedDriverRegistered());
         TestConfiguration.getCurrent().shutdownEngine();
         
-        // DERBY-2905 - Autoload driver is left around.
-        // assertFalse(isEmbeddedDriverRegistered());   
+        // DERBY-2905 - Autoload driver is [not] left around.
+        assertFalse(isEmbeddedDriverRegistered());   
     }
     
     /**
@@ -225,7 +280,7 @@ public class AutoloadTest extends BaseJD
     }
 
 	/**
-     * Test we can connect successfully to a database.
+     	 * Test we can connect successfully to a database.
 	 */
 	public void testSuccessfulConnect()
        throws SQLException
@@ -296,7 +351,7 @@ public class AutoloadTest extends BaseJD
             testSuccessfulConnect();
             testUnsuccessfulConnect();
         }
-        
+
         Class.forName(driverClass).newInstance();
         testSuccessfulConnect();
         testUnsuccessfulConnect();
@@ -387,7 +442,49 @@ public class AutoloadTest extends BaseJD
     {
         assertTrue(hasDerbyThreadGroup());
     }
-    
+
+    private boolean getRegisteredDrivers(String driver) {
+
+	Enumeration e = DriverManager.getDrivers();
+
+        while(e.hasMoreElements())
+        {
+                Driver drv = (Driver)e.nextElement();
+                if(drv.getClass().getName().equals(driver))	
+			return true;
+        }
+
+	return false;
+    }
+
+    private void assertShutdownOK() throws SQLException {
+
+        Connection conn = getConnection();
+
+        if (usingEmbedded()) {
+            DataSource ds = JDBCDataSource.getDataSource();
+            JDBCDataSource.setBeanProperty(ds, "shutdownDatabase", "shutdown");
+            try {
+                ds.getConnection();
+                fail("expected shutdown to fail");
+            } catch (SQLException e) {
+                // expect 08006 on successful shutdown
+                assertSQLState("08006", e);
+            }
+            assertTrue(conn.isClosed());
+        } else if (usingDerbyNetClient()) {
+            DataSource ds = JDBCDataSource.getDataSource();
+            JDBCDataSource.setBeanProperty(ds, "connectionAttributes","shutdown=true");
+            try {
+                ds.getConnection();
+                fail("expected shutdown to fail");
+            } catch (SQLException e) {
+                // expect 08006 on successful shutdown
+                assertSQLState("08006", e);
+            }
+        }
+    }
+
     /**
      * Return true if a ThreadGroup exists that has a name
      * starting with 'derby.'. This needs to run without a security



Mime
View raw message