无法删除嵌入式数据库的Derby系统目录

时间:2017-11-30 21:22:36

标签: java apache derby embedded-database

在Windows计算机上为Derby Embedded数据库调用shutdown = true时,我无法删除system directory

以下是我挑战的最小例子:

package derbytest;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author aelder
 */
public class DerbyTest {

    private static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
    private static final String CONN_URL = "jdbc:derby:EmbeddedDBAudit";

    private static File derbySystemFolder;
    private static final String USER_HOME_DIR = System.getProperty("user.home", ".");

    public static Connection getConnection(boolean createDatabase) throws SQLException {
        return DriverManager.getConnection(CONN_URL + (createDatabase ? ";create=true" : ""));
    }

    public static void shutdownConnectionAndCleanup() {
        try {
            DriverManager.getConnection(CONN_URL + ";shutdown=true");
        } catch (SQLException ex) {
            if (!ex.getSQLState().equals("08006")) {
                ex.printStackTrace();
            }
        }

        deleteFolder(derbySystemFolder);
    }

    public static void deleteFolder(File folder) {
        File[] files = folder.listFiles();
        if (files != null) { //some JVMs return null for empty dirs
            for (File f : files) {
                if (f.isDirectory()) {
                    deleteFolder(f);
                } else {
                    f.delete();
                }
            }
        }
        folder.delete();
    }

    public static void setDerbyHome() {
        setDatabaseFile("");

        int index = 1;
        while (derbySystemFolder.exists()) {
            setDatabaseFile(String.valueOf(index++));
        }

        // Set the db system directory.
        System.setProperty("derby.system.home", derbySystemFolder.getAbsolutePath());
    }

    private static void setDatabaseFile(String auditFolderCount) {
        String databaseFilePATH = USER_HOME_DIR + File.separator + ".EmbeddedDBAudit" + auditFolderCount;

        derbySystemFolder = new File(databaseFilePATH);
        derbySystemFolder.deleteOnExit();
    }

    public static void initDerbyHomeAndDriver() {
        setDerbyHome();

        initDerbyDriverInstance();
    }

    public static void initDerbyDriverInstance() {
        try {
            Class.forName(DRIVER).newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(DerbyTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static boolean tableAlreadyExists(SQLException e) {
        return e.getSQLState().equals("X0Y32");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            initDerbyHomeAndDriver();
            getConnection(true);
            shutdownConnectionAndCleanup();
        } catch (SQLException ex) {
            Logger.getLogger(DerbyTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

我已尝试使用外部库删除文件夹,例如apache的org.apache.commons.io.FileDeleteStrategy.FORCE.delete(file);import org.apache.commons.io.FileUtils.deleteDirectory(file);。即使在数据库关闭后,似乎derby系统仍然挂起到文件上。

所需行为:退出时删除system directory

编辑:

Windows进程资源管理器向我显示在数据库连接关闭后,derby.log仍处于打开状态:

enter image description here

1 个答案:

答案 0 :(得分:1)

假设它的Derby代码本身无法关闭此文件,这对我来说似乎是个错误。

关闭引擎后,Derby显然应关闭其日志并释放其所有引用。

如果您可以访问诸如https://www.eclipse.org/mat/之类的工具,那么您可以找到哪个线程(甚至是哪个堆栈帧?)已将此引用分配给derby.log并且无法释放它。

如果您可以将此行为封装在一个演示它的小型测试程序中,我建议您记录针对Derby(http://db.apache.org/derby/DerbyBugGuidelines.html)的错误,以便开发人员可以自行复制它并找出解决方法。

与此同时,您可以通过禁用derby.log文件或将derby.log文件重定位到另一个目录来解决此问题。

这显然不是一个解决办法,但这可能是行为的改善,以至于这个漏洞不再阻碍你的工作了?以下是有关如何控制derby.log文件的一些文档:https://builds.apache.org/job/Derby-docs/lastSuccessfulBuild/artifact/trunk/out/devguide/cdevdvlp25889.html