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 rh...@apache.org on 2011/04/13 15:26:11 UTC

svn commit: r1091772 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/services/monitor/ engine/org/apache/derby/loc/ shared/org/apache/derby/shared/common/reference/ testing/org/apache/derbyTesting/functionTests/tests/lang/

Author: rhillegas
Date: Wed Apr 13 13:26:11 2011
New Revision: 1091772

URL: http://svn.apache.org/viewvc?rev=1091772&view=rev
Log:
DERBY-4589: If the user tries to connect to a database which is missing service.properties, suggest to her that Derby might have crashed in the middle of creating the database and the solution might be to remove and recreate the database.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/HalfCreatedDatabaseTest.java   (with props)
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/services/monitor/StorageFactoryService.java
    db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml
    db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ErrorCodeTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/services/monitor/StorageFactoryService.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/services/monitor/StorageFactoryService.java?rev=1091772&r1=1091771&r2=1091772&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/services/monitor/StorageFactoryService.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/services/monitor/StorageFactoryService.java Wed Apr 13 13:26:11 2011
@@ -659,8 +659,11 @@ final class StorageFactoryService implem
                                                                              getDirectoryPath( name));
                                 }
                                 else
+                                {
+                                    vetService( storageFactory, name );
                                     throw StandardException.newException(SQLState.SERVICE_DIRECTORY_EXISTS_ERROR,
                                                                          getDirectoryPath( name));
+                                }
                             }
 
                             if (serviceDirectory.mkdirs())
@@ -693,6 +696,22 @@ final class StorageFactoryService implem
         throw StandardException.newException(SQLState.SERVICE_DIRECTORY_CREATE_ERROR, t, name);
     } // end of createServiceRoot
 
+    /**
+       Verify that the service directory looks ok before objecting that the database
+       already exists.
+    */
+    private void    vetService( StorageFactory storageFactory, String serviceName ) throws StandardException
+    {
+        // check for existence of service.properties descriptor file
+        StorageFile    service_properties = storageFactory.newStorageFile( PersistentService.PROPERTIES_NAME );
+
+        if ( !service_properties.exists() )
+        {
+            throw StandardException.newException
+                ( SQLState.MISSING_SERVICE_PROPERTIES, serviceName, PersistentService.PROPERTIES_NAME );
+        }
+    }
+
     private String getDirectoryPath( String name)
     {
         StringBuffer sb = new StringBuffer();

Modified: db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml?rev=1091772&r1=1091771&r2=1091772&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml Wed Apr 13 13:26:11 2011
@@ -3697,6 +3697,13 @@ Guide.
             </msg>
 
             <msg>
+                <name>XBM0A.D</name>
+                <text>The database directory '{0}' exists. However, it does not contain the expected '{1}' file. Perhaps Derby was brought down in the middle of creating this database. You may want to delete this directory and try creating the database again.</text>
+                <arg>directoryName</arg>
+                <arg>servicePropertiesName</arg>
+            </msg>
+
+            <msg>
                 <name>XBM0G.D</name>
                 <text>Failed to start encryption engine. Please make sure you are running Java 2 and have downloaded an encryption provider such as jce and put it in your class path. </text>
             </msg>

Modified: db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java?rev=1091772&r1=1091771&r2=1091772&view=diff
==============================================================================
--- db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java (original)
+++ db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java Wed Apr 13 13:26:11 2011
@@ -164,6 +164,7 @@ public interface SQLState {
 	String SERVICE_MISSING_IMPLEMENTATION		= "XBM02.D";
 	String MISSING_PRODUCT_VERSION				= "XBM05.D";
 	String SERVICE_WRONG_BOOT_PASSWORD			= "XBM06.D";
+	String MISSING_SERVICE_PROPERTIES			= "XBM0A.D";
 	String SERVICE_BOOT_PASSWORD_TOO_SHORT		= "XBM07.D";
 	String MISSING_ENCRYPTION_PROVIDER			= "XBM0G.D";
 	String SERVICE_DIRECTORY_CREATE_ERROR		= "XBM0H.D";

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ErrorCodeTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ErrorCodeTest.java?rev=1091772&r1=1091771&r2=1091772&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ErrorCodeTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ErrorCodeTest.java Wed Apr 13 13:26:11 2011
@@ -163,6 +163,7 @@ public final class ErrorCodeTest extends
         		{"XBM06","Startup failed. An encrypted database cannot be accessed without the correct boot password.  ","45000"},
         		{"XBM07","Startup failed. Boot password must be at least 8 bytes long.","45000"},
         		{"XBM08","Could not instantiate {0} StorageFactory class {1}.","45000"},
+        		{"XBM0A","The database directory '{0}' exists. However, it does not contain the expected '{1}' file. Perhaps Derby was brought down in the middle of creating this database. You may want to delete this directory and try creating the database again.","45000"},
         		{"XBM0G","Failed to start encryption engine. Please make sure you are running Java 2 and have downloaded an encryption provider such as jce and put it in your class path. ","45000"},
         		{"XBM0H","Directory {0} cannot be created.","45000"},
         		{"XBM0I","Directory {0} cannot be removed.","45000"},

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/HalfCreatedDatabaseTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/HalfCreatedDatabaseTest.java?rev=1091772&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/HalfCreatedDatabaseTest.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/HalfCreatedDatabaseTest.java Wed Apr 13 13:26:11 2011
@@ -0,0 +1,154 @@
+/*
+
+ Derby - Class org.apache.derbyTesting.functionTests.tests.lang.HalfCreatedDatabaseTest
+
+ 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.derbyTesting.functionTests.tests.lang;
+
+import java.io.File;
+import java.sql.SQLException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.derbyTesting.junit.BaseTestCase;
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.SecurityManagerSetup;
+import org.apache.derbyTesting.junit.TestConfiguration;
+import org.apache.derbyTesting.junit.JDBC;
+
+/**
+ * <p>
+ * This test confirms Derby's behavior when Derby dies in the middle of creating
+ * a database. See DERBY-4589.
+ * </p>
+ */
+
+public class HalfCreatedDatabaseTest extends BaseJDBCTestCase
+{
+    /////////////////////////////////////////////////////////////////////
+    //
+    //  CONSTANTS
+    //
+    /////////////////////////////////////////////////////////////////////
+
+    private static  final   String  DB_NAME = "hcdt_db";
+    private static  final   String  DB_DIRECTORY = DEFAULT_DB_DIR + File.separator + DB_NAME;
+    private static  final   String  SERVICE_PROPERTIES_FILE_NAME = DB_DIRECTORY + File.separator + "service.properties";
+    private static  final   String  RENAMED_FILE_NAME = DB_DIRECTORY + File.separator + "renamed.properties";
+    
+    /////////////////////////////////////////////////////////////////////
+    //
+    //  STATE
+    //
+    /////////////////////////////////////////////////////////////////////
+    
+    /////////////////////////////////////////////////////////////////////
+    //
+    //  CONSTRUCTOR
+    //
+    /////////////////////////////////////////////////////////////////////
+    
+    public HalfCreatedDatabaseTest(String name) { super(name); }
+    
+    /////////////////////////////////////////////////////////////////////
+    //
+    //  JUnit BEHAVIOR
+    //
+    /////////////////////////////////////////////////////////////////////
+    
+    public static Test suite()
+    {
+        TestSuite suite = new TestSuite("HalfCreatedDatabaseTest");
+
+        suite.addTest( decorateTest() );
+        
+        return suite;
+    }
+    
+    private static Test decorateTest()
+    {
+        Test test = new TestSuite( HalfCreatedDatabaseTest.class );
+
+        test = TestConfiguration.singleUseDatabaseDecorator( test, DB_NAME );
+
+        test = SecurityManagerSetup.noSecurityManager( test );
+
+        return test;
+    }
+
+    /////////////////////////////////////////////////////////////////////
+    //
+    //  TESTS
+    //
+    /////////////////////////////////////////////////////////////////////
+
+    /**
+     * <p>
+     * Test that you get the expected error if you try to connect to a database
+     * which is missing service.properties, the tell-tale sign of an aborted
+     * database creation. See DERBY-4589.
+     * </p>
+     */
+    public  void    test_4589() throws Exception
+    {
+        // make sure the database is created
+        getConnection();
+
+        // now shutdown the database
+        getTestConfiguration().shutdownDatabase();
+
+        //
+        // Now move service.properties aside. This will make it look as
+        // though Derby crashed in the middle of database creation.
+        //
+        File    serviceProperties = new File( SERVICE_PROPERTIES_FILE_NAME );
+        File    renamedProperties = new File( RENAMED_FILE_NAME );
+        serviceProperties.renameTo( renamedProperties );
+
+        // getting a connection should fail
+        try {
+            getConnection();
+            fail( "Should not be able to get a connection." );
+        }
+        catch (SQLException se)
+        {
+            boolean sawCorrectError = false;
+            
+            while ( se != null )
+            {
+                String  sqlstate = se.getSQLState();
+                if ( "XBM0A".equals( sqlstate ) )
+                {
+                    sawCorrectError = true;
+                    break;
+                }
+
+                se = se.getNextException();
+            }
+
+            assertTrue( sawCorrectError );
+        }
+
+        // move service.properties back so that tearDown() won't explode
+        renamedProperties.renameTo( serviceProperties );
+    }
+    
+}
+
+

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

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java?rev=1091772&r1=1091771&r2=1091772&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java Wed Apr 13 13:26:11 2011
@@ -220,6 +220,7 @@ public class _Suite extends BaseTestCase
         suite.addTest(OrderByAndOffsetFetchInSubqueries.suite());
         suite.addTest(Derby5005Test.suite());
         suite.addTest(AutoIncrementTest.suite());
+        suite.addTest(HalfCreatedDatabaseTest.suite());
         return suite;
 	}
 }