猿问

无法使用 DeleteDbFiles.execute 使用 Java 删除我的 h2 数据库文件

我想用 DeleteDbFiles.execute("./data", "mydb", false) 删除 h2 数据库文件,但出现以下错误:org.h2.message.DbException: Cannot delete file "dir/data/mydb.mv.db". 和Caused by: org.h2.jdbc.JdbcSQLNonTransientException: Cannot delete file "dir/data/mydb.mv.db".

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

主要课程:

import...


public class Main {

JalaliCalendar jalaliCalendar = new JalaliCalendar();

EntityManagerFactory emf = Persistence.createEntityManagerFactory("NewPersistenceUnit");

List<LoginEntity> list = new ArrayList<>();


public Main(){

    getData();

    interance();

}


public void interance(){


    if (!list.isEmpty()){


        LoginForm loginForm = new LoginForm(list);

        loginForm.setTitle("پنجره ورود");

        loginForm.setContentPane(loginForm.mainpane);

        loginForm.pack();

        loginForm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        loginForm.setLocationRelativeTo(null);

        loginForm.setVisible(true);


    }else {


        Wellcome wellcome = new Wellcome();

        wellcome.setTitle("خوش آمدید");

        wellcome.setContentPane(wellcome.mainpane);

        wellcome.pack();

        wellcome.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        wellcome.setLocationRelativeTo(null);

        wellcome.setVisible(true);


    }

}


public void getData(){

    EntityManager em = emf.createEntityManager();

    em.getTransaction().begin();

    Query query = em.createQuery("from LoginEntity ");

    list = query.getResultList();

    em.getTransaction().commit();

    em.close();

}

public void getDiff(){

    JalaliCalendar j1 = new JalaliCalendar();

    j1.setYear(1398);

    j1.setMonth(3);

    j1.setDay(5);


    JalaliCalendar j2 = new JalaliCalendar();

    j2.setYear(1398);

    j2.setMonth(2);

    j2.setDay(5);


}



牧羊人nacy
浏览 127回答 1
1回答

湖上湖

只需像下面这样更改我的 persistence.xml 内容,然后在删除文件之前关闭 EntityManagerFactory。<?xml version="1.0" encoding="UTF-8" standalone="yes"?><persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"&nbsp; &nbsp; &nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp; &nbsp; &nbsp;xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&nbsp; &nbsp; &nbsp;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>&nbsp; &nbsp; <property name="hibernate.connection.url" value="jdbc:h2:file:./data/mydata;DB_CLOSE_ON_EXIT=FALSE;FILE_LOCK=NO"/>&nbsp; &nbsp; <property name="hibernate.connection.driver_class" value="org.h2.Driver"/>&nbsp; &nbsp; <property name="hibernate.connection.username" value=""/>&nbsp; &nbsp; <property name="hibernate.connection.password" value=""/>&nbsp; &nbsp; <property name="hibernate.archive.autodetection" value="class"/>&nbsp; &nbsp; <property name="hibernate.show_sql" value="true"/>&nbsp; &nbsp; <property name="hibernate.format_sql" value="true"/>&nbsp; &nbsp; <property name="hbm2ddl.auto" value="update"/></properties></persistence-unit>现在可以使用以下方式删除文件:DeleteDbFiles.execute("./data", "mydb", false);
随时随地看视频慕课网APP

相关分类

Java
我要回答