亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使用 DeleteDbFiles.execute 使用 Java 刪除我的 h2 數據庫文件

無法使用 DeleteDbFiles.execute 使用 Java 刪除我的 h2 數據庫文件

牧羊人nacy 2023-06-08 20:15:20
我想用 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);}
查看完整描述

1 回答

?
湖上湖

TA貢獻2003條經驗 獲得超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>

現在可以使用以下方式刪除文件:


DeleteDbFiles.execute("./data", "mydb", false);


查看完整回答
反對 回復 2023-06-08
  • 1 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號