无法关闭与EntityManagerFactory的德比连接

问题描述

创建后,我需要在zip文件中压缩数据库(并删除数据库的原始版本)。使用EntityManagerFactory创建它之后,我将其关闭,以便释放连接并可以操纵数据库文件

问题在于,即使我关闭EntityManagerFactory,数据库目录仍然被阻止,因为它说它正在使用中(不应该吗?)。

我创建了一个重新创建问题的人...

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Main {

    private static EntityManagerFactory entityManagerFactory;

    public static void main(String[] args) {
        String dbURL = "jdbc:derby:C:\\Users\\XXX\\Desktop\\MyDB;create=true";

        Map<String,String> persistenceMap = new HashMap<String,String>();
        persistenceMap.put("javax.persistence.jdbc.url",dbURL);
    
        entityManagerFactory = Persistence.createEntityManagerFactory("PUBLIC",persistenceMap);        
        entityManagerFactory.close();
        entityManagerFactory = null;
    
        System.out.println("DONE");

        //I should be able to delete the database directory Now (I'm not).
    
        try {
            Thread.sleep(Long.MAX_VALUE);
        } catch (InterruptedException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        }
    }

}

解决方法

问题解决了,我不得不关闭数据库。

try {
        DriverManager.getConnection(
                "jdbc:derby:" + TEMP + "/" + projectName + ";user=xxxx;password=xxxx;shutdown=true");
    } catch (SQLException sqle) {
        //If catches exception went well
    }