hibernate / hsqldb:关闭后找不到数据库文件

时间:2017-02-05 12:14:15

标签: java hibernate hsqldb

当我关闭休眠数据连接时,仍然存在锁定文件。

这就是我的尝试:

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class TestEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;

    @Column public String name = "";

    public TestEntity() { }
}

public class TestWhyNotUnlockDatabase {

    public static void main(String[] args) throws Exception {

            Map<String, String> connProp =      new HashMap<String,String>();
            connProp.put("javax.persistence.jdbc.url","jdbc:hsqldb:file:\\Users\\Bernd\\.mld\\0.2\\solala.db;shutdown=true");
            connProp.put("javax.persistence.schema-generation.database.action", "none");

            System.out.println("Creating DB");
            EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("WhyNotUnLock",connProp);
            EntityManager entityManager = entityManagerFactory.createEntityManager();

            System.out.println("persisting");
            entityManager.getTransaction().begin();
            entityManager.persist(new TestEntity());
            entityManager.getTransaction().commit();

            System.out.println("closing");
            entityManager.clear();
            entityManager.close();
            entityManagerFactory.close();
            System.out.println("DB closed");
    }

}

输出符合预期:

Creating DB
WARN   2017-02-36 12:59:29  [main] org.hibernate.orm.connections.pooling - HHH10001002: Using Hibernate built-in connection pool (not for production use!)
persisting
closing
DB closed

但是在程序执行结束后只有以下文件在这里:

solala.db.properties
solala.db.script

我的问题:

  • 我原本希望数据存储在名为solala.db的文件中,但我找不到。数据库文件在哪里?

  • 其他一些奇怪的观察:我在Windows Eclipse IDE(Neon.2 Release(4.6.2))中执行此操作。运行此程序后退出Exclipse,Eclipse无法再启动。没有给出错误消息。只有重新安装Eclipse才有帮助(重新启动是不够的)。似乎在该计划结束后仍然存在一些人工制品。

其他信息 persitence.xml中的主要部分:

<persistence-unit name="WhyNotUnLock"   transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>masterlist.trials.TestEntity</class>


         <exclude-unlisted-classes>true</exclude-unlisted-classes>

        <properties>
             <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
            <property name="connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />        
            <property name="javax.persistence.jdbc.user" value="SA" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file://C:\\Users\\Bernd\\.mld\\0.1\\smallDB.db"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />

            <property name="hibernate.max_fetch_depth" value="3" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />

            <property name="hibernate.max_fetch_depth" value="3" />
            <property name="hibernate.hbm2ddl.auto" value="create" />

            <!-- Echo all executed SQL to stdout -->
            <property name="hibernate.show_sql" value="false" />
            <property name="javax.persistence.schema-generation.database.action" value="none"/>


        </properties>
    </persistence-unit>

以及build.gradle中的依赖项:

compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.6.Final'
compile group: 'org.hibernate', name: 'hibernate-ehcache', version: '5.2.6.Final'
compile group: 'org.hibernate', name: 'hibernate-validator', version: '5.4.0.CR1'
compile 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final' 
compile group: 'org.hibernate', name: 'hibernate-c3p0', version: '5.2.6.Final' 
compile 'com.enigmabridge:hibernate4-sqlite-dialect:0.1.2'

编辑1(不公开)

有人认为这个问题是enter link description here的一个共同点。我看了看这个答案。不幸的是,我无法从这个链接得到答案。在此链接中,作者需要一个内存数据库,其中所有文件在关闭后都会被删除。相反,我希望在应用程序关闭后数据库文件仍然未被查看。

修改2(shutdown=true

选项shutdown=true已添加到代码中。现在锁定已被删除。无论如何数据库文件仍然缺失。问题标题改编。

1 个答案:

答案 0 :(得分:0)

部分回答: 设置shutdown=true删除了临时文件和锁定文件:

connProp.put("javax.persistence.jdbc.url","jdbc:hsqldb:file:\\Users\\Bernd\\.mld\\0.2\\solala.db;shutdown=true");

不仅剩下两个文件:

solala.db.properties
solala.db.script

我仍在搜索solala.db