【问题标题】:How can I shut down a Derby connection that has been made by another program?如何关闭由另一个程序建立的 Derby 连接?
【发布时间】:2013-12-29 00:09:11
【问题描述】:

我编写了一个测试程序,它建立一个 Derby 连接并在必要的表不存在时创建它们。但是一旦我运行该程序,我就无法再次运行它,因为我带来了异常。我认为我应该向当前的单音类添加一个 finilize 方法并关闭那里的连接。这是解决方案吗?

但无论如何,我认为程序崩溃并以异常方式退出程序的可能性。在这种情况下,如何强制关闭与数据库的连接的其他实例并建立自己的连接?

public class DBTest {

    public static void main(String[] args) {
        DBConnection.getInstance();
    }
}


public class DBConnection {
    private static DBConnection ourInstance = new DBConnection();

    private Connection connection;

    public static DBConnection getInstance() {
        return ourInstance;
    }

    private DBConnection() {

        String driver = "org.apache.derby.jdbc.EmbeddedDriver";
        String dbName="C:/Users/Puk/IdeaProjects/DerbyTest/database/Ablauf";
        String connectionURL = "jdbc:derby:" + dbName + ";create=true";
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            connection = DriverManager.getConnection(connectionURL);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        createTables();
    }

    private void createTables(){
        Statement s;
        try {
            s = connection.createStatement();
            s.execute("CREATE TABLE category ( id  SMALLINT NOT NULL GENERATED ALWAYS AS IDENTITY \n" +
                    "\t(START WITH 0, INCREMENT BY 1), title  VARCHAR(100))");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

java.sql.SQLException: Failed to start database 'C:/Users/Puk/IdeaProjects/DerbyTest/database/Ablauf' with class loader sun.misc.Launcher$AppClassLoader@1830e4a7, see the next exception for details.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.seeNextException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.bootDatabase(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.<init>(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection40.<init>(Unknown Source)
    at org.apache.derby.jdbc.Driver40.getNewEmbedConnection(Unknown Source)
    at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
    at org.apache.derby.jdbc.Driver20.connect(Unknown Source)
    at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:243)
    at model.DBConnection.<init>(DBConnection.java:31)
    at model.DBConnection.<clinit>(DBConnection.java:12)
    at test.DBTest.main(DBTest.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.sql.SQLException: Failed to start database 'C:/Users/Puk/IdeaProjects/DerbyTest/database/Ablauf' with class loader sun.misc.Launcher$AppClassLoader@1830e4a7, see the next exception for details.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
    ... 20 more
Caused by: java.sql.SQLException: Another instance of Derby may have already booted the database C:\Users\Puk\IdeaProjects\DerbyTest\database\Ablauf.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    ... 17 more
Caused by: ERROR XSDB6: Another instance of Derby may have already booted the database C:\Users\Puk\IdeaProjects\DerbyTest\database\Ablauf.
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.privGetJBMSLockOnDB(Unknown Source)
    at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.getJBMSLockOnDB(Unknown Source)
    at org.apache.derby.impl.store.raw.data.BaseDataFileFactory.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.TopService.bootModule(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(Unknown Source)
    at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Unknown Source)
    at org.apache.derby.impl.store.raw.RawStore.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.TopService.bootModule(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(Unknown Source)
    at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Unknown Source)
    at org.apache.derby.impl.store.access.RAMAccessManager.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.TopService.bootModule(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startModule(Unknown Source)
    at org.apache.derby.iapi.services.monitor.Monitor.bootServiceModule(Unknown Source)
    at org.apache.derby.impl.db.BasicDatabase.bootStore(Unknown Source)
    at org.apache.derby.impl.db.BasicDatabase.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.boot(Unknown Source)
    at org.apache.derby.impl.services.monitor.TopService.bootModule(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.bootService(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startProviderService(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.findProviderAndStartService(Unknown Source)
    at org.apache.derby.impl.services.monitor.BaseMonitor.startPersistentService(Unknown Source)
    at org.apache.derby.iapi.services.monitor.Monitor.startPersistentService(Unknown Source)
    ... 17 more
Exception in thread "main" java.lang.ExceptionInInitializerError
    at test.DBTest.main(DBTest.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.NullPointerException
    at model.DBConnection.createTables(DBConnection.java:41)
    at model.DBConnection.<init>(DBConnection.java:35)
    at model.DBConnection.<clinit>(DBConnection.java:12)
    ... 6 more

Process finished with exit code 1

【问题讨论】:

  • 后台某处会有死进程,通过任务管理器将其杀死。它将被命名为 java 或 javaw,您可能需要先关闭 IDE,以免无意中杀死它。话虽如此,你应该真的改进你的错误处理,因为现在你除了把错误写在屏幕上之外什么都不做,然后继续看看下一行崩溃并烧毁。
  • @fvu 我认为如果我杀死 java,我会杀死所有的 java 程序,包括我的 IDE。该程序用于测试,一个小时前我从未使用 Derby 编写过程序,因此感谢错误处理注释,但它们必须在最终代码中替换为 JOptionPane/System.exit(0)。更重要的是用户何时使用该程序。她/他不知道必须在任务管理器中杀死该进程,而我必须为他/她这样做。
  • 不,每个 java 进程都加载了单独的 java 或 javaw 进程。而且您应该使您的程序能够正确处理错误并正确地与数据库分离,当然我并不是建议您的用户应该这样做。它现在发生的事实很可能与不存在的错误处理和恢复直接相关,因此您应该解决该问题。如果您想阅读一些东西,official tutorial on JDBC 非常好。
  • 在现实生活中,崩溃的程序在绝大多数情况下都会从内存中消失,进程在调试期间卡住或在 IDE 下运行更为常见。但真的相信我,预防是游戏的名称,你应该真正建立体面的错误处理和恢复。这应该可以解决您将遇到的 99.99% 的问题。
  • 作为 Netbeans 用户,我可以确认 Netbeans 偶尔也会丢失正在开发的程序 - 这只是与开发相关的这些小烦恼之一,通过任务管理器杀死它们并不是什么大不了的事,我只需敲击我的 IDE 而不是正在测试的程序,您就会很快掌握它的窍门!祝你的项目好运。

标签: java jdbc database-connection derby sqlexception


【解决方案1】:

您不能同时从多个 jvm 启动一个数据库。这在文档Derby Dev Guide

中有解释

HTH

D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 2016-11-12
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多