簡體   English   中英

無法使用java使用Backup.execute備份我的h2數據庫

[英]can not backup my h2 database with Backup.execute using java

我想使用Backup.execute(“ name.zip”,“ ./data","mydb”,true)備份h2數據庫文件,但是出現以下錯誤:java.io.IOException:該進程無法訪問該文件,因為另一個進程已鎖定文件的一部分。

我試圖關閉EntityManager em(em.close())和EntityManagerFactory emf(emf.close()),然后刪除文件,但仍然遇到相同的錯誤。 我試圖從mainForm刪除@persistencecontext,但仍然收到相同的錯誤。 這是我的代碼:

我所有的代碼都在這里: 無法使用java的DeleteDbFiles.execute刪除我的h2數據庫文件

對於誤會我深表歉意。 您錯過的是參數的順序。 應該是下面的樣子。

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

只需像下面那樣更改我的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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM