英文:
Cannot close derby connection with EntityManagerFactory
问题
我需要在创建数据库后(并删除原始版本的数据库)将其压缩到一个 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");
        //我现在应该能够删除数据库目录(但实际上不能)。
        try {
            Thread.sleep(Long.MAX_VALUE);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
英文:
I need to compress a database inside a zip file after it's been created (and delete the original version of the database). After creating it using EntityManagerFactory I close it so that the connection is freed and I can manipulate the database file.
The problem is that even if I close the EntityManagerFactory the database directory is still blocked because it says it is in use (it shouldn't?).
I created a mre recreating the problem...
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();
	    }
    }
}
答案1
得分: 1
问题已解决,我必须关闭数据库。
try {
    DriverManager.getConnection(
        "jdbc:derby:" + TEMP + "/" + projectName + ";user=xxxx;password=xxxx;shutdown=true");
} catch (SQLException sqle) {
    // 如果捕捉到异常,说明操作顺利
}
英文:
Problem solved, I had to shutdown the database.
try {
        DriverManager.getConnection(
                "jdbc:derby:" + TEMP + "/" + projectName + ";user=xxxx;password=xxxx;shutdown=true");
    } catch (SQLException sqle) {
        //If catches exception went well
    }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论