⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 derbynetautostart.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    passed = false;                    return;                }            }        }        try        {            String[] properties;            if( portNumber <= 0)                properties = new String[] {Property.START_DRDA, "true"};            else                properties = new String[] {Property.START_DRDA, "true",                                           Property.DRDA_PROP_PORTNUMBER, 										   String.valueOf( portNumber)};            portNumber = -1;            if( runTest( properties))            {                checkConn( drdaConn, "network");                checkConn( embeddedConn, "embedded");                drdaConn.close();                                    endTest( false);                // There should be a warning in the derby.log file.                try                {                    // The network server is started in a different thread, so give it a little time                    // to write the message                    Thread.sleep(1000);                    logFile = new RandomAccessFile( logFileName, "r");                    if( appendingToLog)                        logFile.seek( startLogFileLength);                }                catch( Exception e)                {                    System.out.println( "Cannot open derby.log: " + e.getMessage());                    passed = false;                    drdaConn.close();                    stopServer( serverProcess);                    return;                }                if( !checkLog( logFileName, new String[] {"An exception was thrown during network server startup"}))                {                    // Was the network server started? Print out the names of the threads                    System.out.println( "Active threads:");                    ThreadGroup tg = Thread.currentThread().getThreadGroup();                    while( tg.getParent() != null)                        tg = tg.getParent();                    Thread[] activeThreads = new Thread[ 16*Thread.activeCount()];                    int threadCount = tg.enumerate( activeThreads, true);                    for( int idx = 0; idx < threadCount; idx++)                        System.out.println( "  " + activeThreads[idx].getName());                    // Is the server process still running?                    try                    {                        int ev = serverProcess.exitValue();                        System.out.println( "The separate server process exited prematurely with code " + ev);                    }                    catch( IllegalThreadStateException itse)                    {                        System.out.println( "The server process seems to be running.");                    }                }            }        }        catch( Exception e)        {            e.printStackTrace();            passed = false;        }        stopServer( serverProcess);    } // end of testExtantNetServer    private static long getLogFileLength( String logFileName)    {        try        {            RandomAccessFile logFile = new RandomAccessFile( logFileName, "r");            long length = logFile.length();            logFile.close();            return length;        }        catch( Exception e){ return 0;}    }    private static void checkConn( Connection conn, String label)    {        try        {            DatabaseMetaData dbmd = conn.getMetaData();            ResultSet rs = dbmd.getSchemas();            while( rs.next());            rs.close();        }        catch( SQLException sqle)        {            passed = false;            System.out.println( "Could not use the " + label + " connection:");            System.out.println( "  " + sqle.getMessage());        }    } // end of checkConn    private static void stopServer( Process serverProcess)    {        try        {            NetworkServerControl server =				new NetworkServerControl(InetAddress.getByName("localhost"),									 portNumber);			server.shutdown();            Thread.sleep(5000);        }        catch( Exception e)        {            System.out.println( "  Exception thrown while trying to shutdown the remote server.");            System.out.println( "    " + e.getMessage());            passed = false;        }        serverProcess.destroy();    } // end of stopServer            private static boolean checkLog( String logFileName, String[] expected) throws IOException    {        boolean allFound = true;        boolean[] found = new boolean[ expected.length];        FileInputStream is = new FileInputStream(logFileName);        BufferedReader br = new BufferedReader(new InputStreamReader(is));        String logLine;         while((logLine = br.readLine()) != null)                    {            for( int i = 0; i < expected.length; i++)            {                if( (! found[i]) && logLine.indexOf( expected[i]) >= 0)                    found[i] = true;            }        }        for( int i = 0; i < expected.length; i++)        {            if( ! found[i])            {                passed = false;                System.out.println( "Derby.log does not contain\n  '" + expected[i] + "'.");                allFound = false;            }        }        return allFound;    } // end of checkLog    private static boolean startTest( String[] properties)    {        announceTest();        return runTest( properties);    }    private static boolean runTest( String[] properties)    {        drdaConn = null;        embeddedConn = null;        if( !writeDerbyProperties( properties))            return false;                deleteDir( homeDir + File.separatorChar + databaseName);        try        {            System.setOut( serverOutputOut);            Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();            embeddedConn = DriverManager.getConnection( "jdbc:derby:" + databaseName + ";create=true");            System.setOut( realSystemOut);        }        catch( SQLException sqle)        {            System.setOut( realSystemOut);            passed = false;            System.out.println( "  Could not create an embedded database.");            System.out.println( "    " + sqle.getMessage());            return false;        }        catch( Exception e)        {            System.setOut( realSystemOut);            passed = false;            System.out.println( "  Could not start the Derby client driver.");            System.out.println( "    " + e.getMessage());            return false;        }        if( portNumber > 0)        {            for( int ntries = 1; ; ntries++)            {                try                {                    Thread.sleep(1000); // Give the server more time to start                }                catch( InterruptedException ie) {}                try                {                    drdaConn = DriverManager.getConnection(														   TestUtil.getJdbcUrlPrefix("localhost",portNumber) + databaseName,														   authenticationProperties);                    break;                }                catch( SQLException sqle)                {                    if( ntries > 5)                    {                        passed = false;                        System.out.println( "  Could not access database through the network server.");                        System.out.println( "    " + sqle.getMessage());                        return false;                    }                }            }        }        return true;    } // end of startTest    private static boolean writeDerbyProperties( String[] properties)    {        derbyPropertiesFile.delete();        try        {            derbyPropertiesFile.createNewFile();            PrintStream propertyWriter = new PrintStream( new FileOutputStream( derbyPropertiesFile));            propertyWriter.print( basePropertiesSB.toString());            for( int i = 0; i < properties.length - 1; i += 2)                propertyWriter.println( properties[i] + "=" + properties[i + 1]);            propertyWriter.close();            return true;        }        catch( IOException ioe)        {            passed = false;            System.out.println( "  Could not create derby.properties: " + ioe.getMessage());            return false;        }    } // end of writeDerbyProperties    private static void deleteDir( String dirName)    {        deleteDir( new File( dirName));    }    private static void deleteDir( File parent)    {        if( ! parent.exists())            return;        if( parent.isDirectory())        {            String[] child = parent.list();            for( int i = 0; i < child.length; i++)                deleteDir( new File( parent, child[i]));        }        parent.delete();    }    private static void announceTest()    {        testNumber++;        System.out.println( "Starting test case " + testNumber + ".");        databaseName = "database" + testNumber;    }            private static void endTest( boolean autoStarted)    {        try        {            if( drdaConn != null)            {                drdaConn.close();                drdaConn = null;            }            if( embeddedConn != null)            {                embeddedConn.close();                embeddedConn = null;            }        }        catch( SQLException sqle)        {            passed = false;            System.out.println( "  Connection close failed:");            System.out.println( "    " + sqle.getMessage());        }        try        {            DriverManager.getConnection( "jdbc:derby:;shutdown=true");        }        catch( SQLException sqle)        {            if( ! sqle.getSQLState().equals( "XJ015"))            {                passed = false;                System.out.println( "  System shutdown failed:");                System.out.println( "    " + sqle.getMessage());            }        }        serverOutputOut.flush();        if( serverOutputBOS.size() > 0)        {            passed = false;            System.out.println( "The auto-started server wrote to System.out.");        }        serverOutputBOS.reset();        if( autoStarted && databaseName != null)        {            try            {                // Give the server thread time to shutdown, then check that it has done so.                try                {                    Thread.sleep( 500);                }                catch( InterruptedException ie){};                drdaConn = DriverManager.getConnection(													   TestUtil.getJdbcUrlPrefix("localhost", portNumber) +  databaseName,                                                        authenticationProperties);                passed = false;                System.out.println( "Was able to connect to the network server after Derby shut down.");                drdaConn.close();                drdaConn = null;            }            catch( SQLException sqle){};        }    } // end of endTest	private static boolean isServerStarted(NetworkServerControl server)	{		try {			server.ping();		}		catch (Exception e) {			return false;		}		return true;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -