无法使用EntityManagerFactory关闭derby连接。

huangapple go评论89阅读模式
英文:

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 = &quot;jdbc:derby:C:\\Users\\XXX\\Desktop\\MyDB;create=true&quot;;

	    Map&lt;String, String&gt; persistenceMap = new HashMap&lt;String, String&gt;();
        persistenceMap.put(&quot;javax.persistence.jdbc.url&quot;, dbURL);
    
        entityManagerFactory = Persistence.createEntityManagerFactory(&quot;PUBLIC&quot;, persistenceMap);        
        entityManagerFactory.close();
        entityManagerFactory = null;
    
        System.out.println(&quot;DONE&quot;);

        //I should be able to delete the database directory now (I&#39;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(
                &quot;jdbc:derby:&quot; + TEMP + &quot;/&quot; + projectName + &quot;;user=xxxx;password=xxxx;shutdown=true&quot;);
    } catch (SQLException sqle) {
        //If catches exception went well
    }

huangapple
  • 本文由 发表于 2020年9月11日 19:21:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63846082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定