folder/file 首次连接到 derby 时删除不起作用

folder/file deletion not working when first connected to derby

更新:看起来 system.exit(0) 正在解锁文件并允许第二个代码示例工作。如何在不调用 system.exit() 的情况下解锁文件?

我有两个代码示例。首先创建一个数据库,关闭连接然后尝试删除数据库。尝试删除目录文件夹时失败。

第二个代码示例只是删除文件夹。它有效并且与第一个代码示例中使用的代码相同。

我认为可能存在某种类型的计时问题。即使在第一个示例中发生删除之前连接已关闭,我想知道删除操作是否由于某些潜在连接而无法正常工作。

第一个代码示例:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.io.FileUtils;
import java.io.*;

public class DBtest{        
    public static void main(String[] args) {
        System.out.print('\u000C');
        String db = "test2";

        File file = new File(db);
        Connection conn = getDB(db);  

        try{ 
            if (conn != null) conn.close();
            System.out.println("connection closed " );
        }catch(SQLException e){
            System.out.println("connection NOT closed " + e);
            System.exit(0);
        }

        deleteDirectory(file);
        System.exit(0);

    }
    public static boolean deleteDirectory(File directoryToBeDeleted){
        File[] allContents = directoryToBeDeleted.listFiles();
        if (allContents != null) {
            for (File file : allContents) {           
               deleteDirectory(file);
            }
        }
        directoryToBeDeleted.setWritable(true);
        System.out.println(directoryToBeDeleted.toString());
        return directoryToBeDeleted.delete();
    }   

    public static Connection getDB(String database) {
        try{ 
            /* jdbc:derby specifies the driver to use to connect to the derby database
             * database is the name of the database we want to connect to.  A database can hold many tables
             * create=true is an option that creates and connects to the database if it does not exist, 
             * or just connect if it already exists.
             */ 

            File db =new File(database);            

            String URL = "jdbc:derby:" + database + ";create=true"; 

            System.out.println("db exists " + db.exists());

            Connection conn = DriverManager.getConnection(URL);            
            System.out.println("Succesfully connected to " + database);

            return conn;
        }catch(SQLException e){
            System.out.println("FATAL ERROR: from getDB " + e);

             return null;
        }   
    }    
}

第二个代码示例:

import org.apache.commons.io.FileUtils;
import java.io.*;
public class DirectoryDelete
{
    public static void main(String[] args){
        System.out.println('\u000C');
        File file = new File("test2");
        deleteDirectory(file);

    }

    public static boolean deleteDirectory(File directoryToBeDeleted){
        File[] allContents = directoryToBeDeleted.listFiles();
        if (allContents != null) {
           for (File file : allContents) {           
            deleteDirectory(file);
            }
        }
        directoryToBeDeleted.setWritable(true);
        System.out.println(directoryToBeDeleted.toString());
        return directoryToBeDeleted.delete();
    }
}

在第一个示例中,我需要做些什么来释放 folder/files 以便能够删除它们吗?

好吧,这需要大量的挖掘。原来你必须关闭数据库才能删除文件夹。实际上不需要关闭连接,因为关闭数据库将关闭所有连接。只需调用 DriverManager.getConnection("jdbc:derby:;shutdown=true"); 即可开始比赛。