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

Unable to delete Derby System Directory for 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 在数据库连接关闭后仍然打开:

假设是 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