创建数据库后,我需要将其压缩到zip文件中(并删除数据库的原始版本(。在使用EntityManagerFactory创建它之后,我关闭它,以便释放连接,并可以操作数据库文件。
问题是,即使我关闭EntityManagerFactory,数据库目录仍然被阻止,因为它说它正在使用中(不应该?(。
我创建了一个重现问题的mre。。。
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
}