【问题标题】:can not backup my h2 database with Backup.execute using java无法使用 java 使用 Backup.execute 备份我的 h2 数据库
【发布时间】:2019-08-23 12:46:09
【问题描述】:

我想使用 Backup.execute("name.zip", "./data","mydb", true ) 备份 h2 数据库文件,但出现以下错误:java.io.IOException: The process cannot access the file因为另一个进程锁定了文件的一部分。

我尝试关闭 EntityManager em (em.close()) 和 EntityManagerFactory emf (emf.close()) 然后删除文件但仍然出现相同的错误。我试图从 mainForm 中删除 @persistencecontext 但仍然遇到同样的错误。这是我的代码:

我所有的代码都在这里: can not delete my h2 database files with DeleteDbFiles.execute using java

【问题讨论】:

    标签: java jpa h2


    【解决方案1】:

    对于这种误解,我深表歉意。您错过的是参数的顺序。应该如下所示。

    Backup.execute("/location/of/backup/file" + "/backup.zip", "./data", "yourdbname", true);
    

    【讨论】:

    • 这不是真的,H2有在线备份命令。 h2database.com/html/commands.html#backup
    • 您说在您的答案的第一个版本中使用数据库时无法进行备份。不正确,只有备份工具在使用数据库时不能使用。 BACKUP 命令 需要具有 ADMIN 权限的活动连接,并且可以改为使用它。针对不同的用例有不同的工具。
    • 您编辑的答案是正确的,因为它明确提到不能使用 工具。并不是说不能在线备份了。
    • 它也不允许我删除数据库文件:stackoverflow.com/questions/57546917/…
    • 如何关闭主应用程序然后进行备份?
    【解决方案2】:

    只需更改我的 persistence.xml 内容,如下所示,然后在备份之前关闭 EntityManagerFactory。

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    <persistence-unit name="NewPersistenceUnit">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>model.MycardsEntity</class>
        <class>model.PeymentsEntity</class>
        <class>model.WorkersEntity</class>
        <class>model.YourcardsEntity</class>
        <class>model.LoginEntity</class>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:h2:file:./data/mydata;DB_CLOSE_ON_EXIT=FALSE;FILE_LOCK=NO"/>
            <property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
            <property name="hibernate.connection.username" value=""/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
    

    现在可以使用此代码进行备份:

            emf.close();
        try {
            Backup.execute("./backup/name.zip", "./data", "mydb", true);
            System.out.println("done");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      相关资源
      最近更新 更多