You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by li...@apache.org on 2011/01/27 06:08:01 UTC

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

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



Re: 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

Posted by Lily Wei <li...@yahoo.com>.
Thank you so much for the kind words, Dag.
 
On another subject: Thank you for fixing the interrupt issues. It is a very 
hairy issue. 



________________________________
From: Dag H. Wanvik <da...@oracle.com>
To: Derby Development <de...@db.apache.org>
Sent: Thu, January 27, 2011 7:12:54 AM
Subject: Re: 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

Thanks for fixing this issue, Lily!

lilywei@apache.org writes:

> DERBY-2905 Shutting down embedded Derby will remove AutoloadedDriver. The 
>AutoloadedDriver is not left registered in the DriverManager.

I think you changed the meaning of the issue title here to reflect the
change you have made? Confused me at first :)

Original text is "Shutting down embedded Derby does not remove all code, the 
AutoloadDriver is left registered in the DriverManager."

Typically, we have used the original issue title in the commit
message. Just thought I'd mention it :)

>>Yes, I will use the original issue title in the commit message in the future and 
>>reflect my changes after that.
>>Thank you for letting me knows and the good eyes. 


      

Re: 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

Posted by "Dag H. Wanvik" <da...@oracle.com>.
Thanks for fixing this issue, Lily!

lilywei@apache.org writes:

> DERBY-2905 Shutting down embedded Derby will remove AutoloadedDriver. The AutoloadedDriver is not left registered in the DriverManager.

I think you changed the meaning of the issue title here to reflect the
change you have made? Confused me at first :)

Original text is "Shutting down embedded Derby does not remove all code, the AutoloadDriver is left registered in the DriverManager."

Typically, we have used the original issue title in the commit
message. Just thought I'd mention it :)

Thanks,
Dag