hibernate/hsqldb: 关机后找不到数据库文件
hibernate/hsqldb: Database file not found after shutdown
当我关闭休眠数据连接时,仍然存在一个锁定文件。
这是我试过的:
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 版本(4.6.2))中执行此操作。 运行这个程序退出Exclipse后,Eclipse就不能再启动了。没有给出错误信息。只有 re-install 的 Eclipse 有帮助(re-boot 是不够的)。似乎在该程序结束后仍然存在一些人工制品。
附加信息
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 重复。我看了这个答案。不幸的是,我无法从这个 link 中得到答案。在这个 link 中,作者想要一个内存数据库,其中所有文件在关机后都会被删除。相反,我希望数据库文件在应用程序关闭后仍然存在。
编辑 2 (shutdown=true
)
选项 shutdown=true
已添加到代码中。现在锁定被移除。无论如何,数据库文件仍然丢失。问题标题已修改。
部分回答:
设置 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
当我关闭休眠数据连接时,仍然存在一个锁定文件。
这是我试过的:
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 版本(4.6.2))中执行此操作。 运行这个程序退出Exclipse后,Eclipse就不能再启动了。没有给出错误信息。只有 re-install 的 Eclipse 有帮助(re-boot 是不够的)。似乎在该程序结束后仍然存在一些人工制品。
附加信息
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 重复。我看了这个答案。不幸的是,我无法从这个 link 中得到答案。在这个 link 中,作者想要一个内存数据库,其中所有文件在关机后都会被删除。相反,我希望数据库文件在应用程序关闭后仍然存在。
编辑 2 (shutdown=true
)
选项 shutdown=true
已添加到代码中。现在锁定被移除。无论如何,数据库文件仍然丢失。问题标题已修改。
部分回答:
设置 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