While working on a fix for DERBY-1823 (a documentation issue concerning user authentication and authorization), I've tried to write a sample Java program that connects to a database using the embedded driver, does some things, shuts it down, starts it up, does some more things, shuts it down again, and so on. I always get an error trying to connect to the database the second time, although I've followed the advice in "Shutting down the system" (http://db.apache.org/derby/docs/dev/devguide/tdevdvlp20349.html) to run System.gc() between the first shutdown and the restart so as to unload the embedded driver.

I've attached a simplified version of the program, one that just does the repeated startups and shutdowns. The error is shown below. What am I doing wrong?

If I break up all my steps into 5 smaller programs, they all work fine, because there's only one startup and shutdown per program. But it would be handier to be able to combine them in one.

Thanks,
Kim Haase

-----------

179 =>java SimpleExample
org.apache.derby.jdbc.EmbeddedDriver loaded.
Trying to connect to jdbc:derby:jdbcDemoDB;create=true
Connected to database jdbc:derby:jdbcDemoDB;create=true
Closed connection
Database shut down normally
org.apache.derby.jdbc.EmbeddedDriver loaded.
Trying to connect to jdbc:derby:jdbcDemoDB

---SQLException Caught---

SQLState:   null
Severity: 0
Message: org.apache.derby.jdbc.EmbeddedDriver is not registered with the JDBC driver manager java.sql.SQLException: org.apache.derby.jdbc.EmbeddedDriver is not registered with the JDBC driver manager at org.apache.derby.jdbc.AutoloadedDriver.getDriverModule(Unknown Source)
        at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source)
        at java.sql.DriverManager.getConnection(DriverManager.java:525)
        at java.sql.DriverManager.getConnection(DriverManager.java:193)
        at SimpleExample.main(SimpleExample.java:74)
import java.sql.*;

public class SimpleExample {

    public static void main(String[] args) {
    
        String driver = "org.apache.derby.jdbc.EmbeddedDriver";
        String dbName="jdbcDemoDB";
        String connectionURL = "jdbc:derby:" + dbName + ";create=true";
        Connection conn = null;

        // Load the driver
        try {
            Class.forName(driver);
            System.out.println(driver + " loaded.");
        } catch (java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
            System.out.println("\n Make sure your CLASSPATH variable " +
                "contains %DERBY_HOME%\\lib\\derby.jar (${DERBY_HOME}/lib/derby.jar). \n");
        }

        // Connect to the database, then close it
        try {
            System.out.println("Trying to connect to " + connectionURL);
            conn = DriverManager.getConnection(connectionURL);
            System.out.println("Connected to database " + connectionURL);
            
            // Shut down the database
            conn.close();
            System.out.println("Closed connection");

            /* In embedded mode, an application should shut down Derby.
               Shutdown throws the XJ015 exception to confirm success. */			
            boolean gotSQLExc = false; 
            try {
                DriverManager.getConnection("jdbc:derby:;shutdown=true");
            } catch (SQLException se) {
                if ( se.getSQLState().equals("XJ015") ) {		
                    gotSQLExc = true;
                }
            }
            if (!gotSQLExc) {
                 System.out.println("Database did not shut down normally");
            } else {
                 System.out.println("Database shut down normally");	
            }

            // force garbage collection to unload the EmbeddedDriver
            //  so Derby can be restarted
            System.gc();
            Thread.sleep(1500);
        } catch (Throwable e) {
            errorPrint(e);
            System.exit(1);
        }
                        
        // Restart database w/o create=true
        connectionURL = "jdbc:derby:" + dbName;

        // Load the driver again?
        try {
            Class.forName(driver);
            System.out.println(driver + " loaded.");
        } catch (java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
            System.out.println("\n Make sure your CLASSPATH variable " +
                "contains %DERBY_HOME%\\lib\\derby.jar (${DERBY_HOME}/lib/derby.jar). \n");
        }

        try {
            System.out.println("Trying to connect to " + connectionURL);
            conn = DriverManager.getConnection(connectionURL);
            System.out.println("Connected to database " + dbName);

            // Shut down the database
            conn.close();
            System.out.println("Closed connection");
            /* In embedded mode, an application should shut down Derby.
               Shutdown throws the XJ015 exception to confirm success. */			
            boolean gotSQLExc = false; 
            try {
                DriverManager.getConnection("jdbc:derby:;shutdown=true");
            } catch (SQLException se) {
                if ( se.getSQLState().equals("XJ015") ) {		
                    gotSQLExc = true;
                }
            }
            if (!gotSQLExc) {
                 System.out.println("Database did not shut down normally");
            } else {
                 System.out.println("Database shut down normally");	
            }
        } catch (Throwable e) {
            errorPrint(e);
            System.exit(1);
        }
    }

    /** Exception reporting methods
     *   with special handling of SQLExceptions
     */
    static void errorPrint(Throwable e) {
        if (e instanceof SQLException) 
            SQLExceptionPrint((SQLException)e);
        else {
            System.out.println("A non-SQL error occurred.");
            e.printStackTrace();
        }   
    }  // END errorPrint 

    //  Iterates through a stack of SQLExceptions 
    static void SQLExceptionPrint(SQLException sqle) {
        while (sqle != null) {
            System.out.println("\n---SQLException Caught---\n");
            System.out.println("SQLState:   " + (sqle).getSQLState());
            System.out.println("Severity: " + (sqle).getErrorCode());
            System.out.println("Message:  " + (sqle).getMessage()); 
            sqle.printStackTrace();  
            sqle = sqle.getNextException();
        }
    }  //  END SQLExceptionPrint   	
}

Reply via email to