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 ka...@apache.org on 2006/07/14 12:15:08 UTC

svn commit: r421856 - in /db/derby/code/trunk/java: drda/org/apache/derby/impl/drda/ testing/org/apache/derbyTesting/functionTests/suites/ testing/org/apache/derbyTesting/functionTests/tests/derbynet/ testing/org/apache/derbyTesting/functionTests/util/

Author: kahatlen
Date: Fri Jul 14 03:15:07 2006
New Revision: 421856

URL: http://svn.apache.org/viewvc?rev=421856&view=rev
Log:
DERBY-1274: Network Server does not shutdown the databases it has
booted when started and shutdown from the command line

Fix submitted by Fernanda Pizzorno.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ShutDownDBWhenNSShutsDownTest.java   (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ShutDownDBWhenNSShutsDownTest_app.properties   (with props)
Modified:
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbynetmats.runall
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java?rev=421856&r1=421855&r2=421856&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/NetworkServerControlImpl.java Fri Jul 14 03:15:07 2006
@@ -290,6 +290,9 @@
 	private Runnable acceptClients;
 	
 
+	// if the server is started from the command line, it should shutdown the
+	// databases it has booted.
+	private boolean shutdownDatabasesOnShutdown = false;
 	
 
 	// constructor
@@ -639,22 +642,23 @@
 			runQueue.notifyAll();
 		}						
 
-		/*
-		// Shutdown Cloudscape
-		try {
-			if (cloudscapeDriver != null)
-				cloudscapeDriver.connect("jdbc:derby:;shutdown=true", 
-										 (Properties) null);
-		} catch (SQLException sqle) {
-			// If we can't shutdown cloudscape. Perhaps authentication is
-			// set to true or some other reason. We will just print a
-			// message to the console and proceed.
-			if (((EmbedSQLException)sqle).getMessageId() !=
-			  SQLState.CLOUDSCAPE_SYSTEM_SHUTDOWN)
-				consolePropertyMessage("DRDA_ShutdownWarning.I",
-									   sqle.getMessage());
+		if (shutdownDatabasesOnShutdown) {
+
+			// Shutdown Cloudscape
+			try {
+				if (cloudscapeDriver != null)
+					cloudscapeDriver.connect("jdbc:derby:;shutdown=true",
+											 (Properties) null);
+			} catch (SQLException sqle) {
+				// If we can't shutdown cloudscape. Perhaps authentication is
+				// set to true or some other reason. We will just print a
+				// message to the console and proceed.
+				if (((EmbedSQLException)sqle).getMessageId() !=
+				  SQLState.CLOUDSCAPE_SYSTEM_SHUTDOWN)
+					consolePropertyMessage("DRDA_ShutdownWarning.I",
+										   sqle.getMessage());
+			}
 		}
-		*/
 
 		consolePropertyMessage("DRDA_ShutdownSuccess.I", new String [] 
 						        {att_srvclsnm, versionString, 
@@ -1712,6 +1716,9 @@
 		switch (command)
 		{
 			case COMMAND_START:
+				// the server was started from the command line, shutdown the
+				// databases when the server is shutdown
+				shutdownDatabasesOnShutdown = true;
 				blockingStart(makePrintWriter(System.out));
 				break;
 			case COMMAND_SHUTDOWN:

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbynetmats.runall
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbynetmats.runall?rev=421856&r1=421855&r2=421856&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbynetmats.runall (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbynetmats.runall Fri Jul 14 03:15:07 2006
@@ -12,6 +12,7 @@
 derbynet/maxthreads.java
 derbynet/prepStmt.java
 derbynet/runtimeinfo.java
+derbynet/ShutDownDBWhenNSShutsDownTest.junit
 derbynet/sysinfo.java
 derbynet/sysinfo_withproperties.java
 derbynet/testProtocol.java

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ShutDownDBWhenNSShutsDownTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ShutDownDBWhenNSShutsDownTest.java?rev=421856&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ShutDownDBWhenNSShutsDownTest.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ShutDownDBWhenNSShutsDownTest.java Fri Jul 14 03:15:07 2006
@@ -0,0 +1,153 @@
+/*
+ *
+ * Derby - Class ShutDownDBWhenNSShutsDownTest
+ *
+ * Copyright 2006 The Apache Software Foundation or its
+ * licensors, as applicable.
+ *
+ * Licensed 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.derbyTesting.functionTests.tests.derbynet;
+
+import java.util.Properties;
+import org.apache.derbyTesting.functionTests.util.BaseTestCase;
+import org.apache.derbyTesting.functionTests.util.TestUtil;
+import org.apache.derbyTesting.functionTests.util.BaseJDBCTestCase;
+import org.apache.derby.drda.NetworkServerControl;
+
+import junit.framework.*;
+import java.sql.*;
+import java.io.PrintWriter;
+import java.io.File;
+import java.security.AccessController;
+
+/**
+ * Derby-1274 - Network Server should shutdown the databases it has booted when
+ * started from the command line.
+ *
+ * Tests that the network server will shutdown the databases it has booted when
+ * started from the command line and that it will not shut down the databases
+ * when started from the API.
+ */
+public class ShutDownDBWhenNSShutsDownTest extends BaseTestCase {
+
+
+    NetworkServerControl server = null;
+
+
+    /**
+     * Creates a new instance of ShutDownDBWhenNSShutsDownTest
+     */
+    public ShutDownDBWhenNSShutsDownTest(String name) {
+        super(name);
+    }
+
+    /**
+     * Test that the NetworkServer shuts down the databases it has booted when
+     * started from the command line, and that it does not shut down the
+     * databases it has booted when started from the API.
+     */
+    public void testDatabasesShutDownWhenNSShutdown()
+            throws Exception
+    {
+        server = new NetworkServerControl();
+        // The server was started from the command line when the test was
+        // started. Check that the database will be shut down when the server
+        // is shut down.
+        shutdownServerCheckDBShutDown(true);
+
+        // Start the server form the API and test that the databases will not
+        // be shutdown when the server is shutdown
+        server.start(null);
+
+        // wait until the server accepts connections
+        int i = 0;
+        while (!pingServer() && i < 10 ) {
+            Thread.sleep(1000);
+            i++;
+        }
+
+        // Check that the databases will not be shutdown when the server is
+        // shut down.
+        shutdownServerCheckDBShutDown(false);
+    }
+
+    /**
+     * Checks whether the server shuts down causes the databases it has booted
+     * to be shut down.
+     *
+     * Creates a database and shuts down the server. If the server was started
+     * from the command line the database should be shut down. If the server
+     * was started from the api the database should not be shut down.
+     *
+     * If the database has been shut down the db.lck file should not exist.
+     *
+     * @param dbShutDown Indicates whether the database should have been shut
+     * down.
+     */
+    private void shutdownServerCheckDBShutDown(boolean dbShutDown)
+            throws Exception
+    {
+        // connect to database
+        createDatabase();
+
+        // shut down the server
+        shutdownServer();
+
+        // check if db.lck exists
+        String fileName = getSystemProperty("derby.system.home") +
+                java.io.File.separator + "wombat" +
+                java.io.File.separator + "db.lck";
+        assertEquals("Database is shut down",
+                dbShutDown,
+                !fileExists(fileName));
+    }
+
+    private boolean fileExists (final String fileName) throws Exception {
+        Boolean b = (Boolean) AccessController.doPrivileged
+            (new java.security.PrivilegedAction(){
+                public Object run(){
+                    File file = new File(fileName);
+                    return new Boolean(file.exists());
+                }
+        });
+
+        return b.booleanValue();
+    }
+
+    private boolean pingServer() {
+		try {
+			server.ping();
+		}
+		catch (Exception e) {
+			return false;
+		}
+		return true;
+    }
+
+    private void createDatabase() throws SQLException {
+        Connection conn = BaseJDBCTestCase.getConnection();
+        conn.setAutoCommit(false);
+        Statement st = conn.createStatement();
+        st.execute("CREATE TABLE T1 (a int)");
+        st.execute("INSERT INTO T1 VALUES (1), (2), (3), (4), (5)");
+        st.execute("DROP TABLE T1");
+        conn.commit();
+    }
+
+    private void shutdownServer() throws Exception {
+        server.shutdown();
+    }
+
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ShutDownDBWhenNSShutsDownTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ShutDownDBWhenNSShutsDownTest_app.properties
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ShutDownDBWhenNSShutsDownTest_app.properties?rev=421856&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ShutDownDBWhenNSShutsDownTest_app.properties (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ShutDownDBWhenNSShutsDownTest_app.properties Fri Jul 14 03:15:07 2006
@@ -0,0 +1,3 @@
+#Don't want harness to shut down network server.
+#It will be shut down in test
+serverNeedsStopping=false

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/ShutDownDBWhenNSShutsDownTest_app.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java?rev=421856&r1=421855&r2=421856&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java Fri Jul 14 03:15:07 2006
@@ -120,5 +120,24 @@
 	
     }
     
+    /**
+     * Get system property.
+     *
+     * @param name name of the property
+     */
+    protected static String getSystemProperty(final String name)
+	throws PrivilegedActionException {
+
+	return (String )AccessController.doPrivileged
+	    (new java.security.PrivilegedAction(){
+
+		    public Object run(){
+			return System.getProperty(name);
+
+		    }
+
+		}
+	     );
+    }
     
 } // End class BaseTestCase