HSQLDB and Hibernate/JPA - not persisting to disk?

坚强是说给别人听的谎言 提交于 2019-11-29 23:02:23

Does anyone have any ideas why the objects aren't getting persisted beyond a single JVM session?

HSQLDB doesn't write changes immediately to disk after a commit (see "WRITE DELAY"), HSQLDB is not Durable by default (that's from where "performances" are coming from).

Either try to set the connection property shutdown=true in the connection string to get the changes written when the last connection will end.

jdbc:hsqldb:file:data/cmon;shutdown=true

If it doesn't help, try to set the WRITE DELAY to 0 (or false). If you're using HSQLDB 1.8.x, use the SQL command:

SET WRITE_DELAY 0

If you're using HSQLDB 2.0.x, you can now also use a connection property hsqldb.write_delay:

jdbc:hsqldb:file:data/cmon;hsqldb.write_delay=false

The solution is :

<property name="dialect">org.hibernate.dialect.HSQLDialect</property>

in hibernate.cfg.xml


This is rest of my configuration:

Libs:

  • HsqlDb 2.0.0
  • Hibernate 3.5.6

Url:

<property name="connection.url">jdbc:hsqldb:file:data/mydb;shutdown=true;hsqldb.write_delay=false;</property>

Did you set hibernate.hbm2ddl.auto to create-drop in your persistence.xml? This drops your tables and re-creates them on every startup.

You can set it to update instead, or if you want to manage the schema yourself, then set it to validate.

Simply close your EntityManagerFactory with HSQL in filemode, after the commit to really persist datas...

I was using HSQL DB version 2.2.5. I tried above approaches i.e. setting shutdown=true and hsqldb.write_delay=false It did not work. As suggested in some blog, I added statement

org.hsqldb.DatabaseManager.closeDatabases(0);

after transaction commit. But it did not work.

HSQL DB version 2.2.9 seems better than this. With one workaround it solves this problem. To handle above problem take following steps :-

1) hsqldb.jar from lib of HSQL DB version 2.2.9

2) In hibernate config xml just specify URL I am using HSQL file-based database.

 <property name="hibernate.connection.url">jdbc:hsqldb:file:D:\JavaProj\TmpDBLocation\myKauDB</property>

3) In your program at the end write statement

org.hsqldb.DatabaseManager.closeDatabases(0);

Now run the hibernate program that commits the data to DB.

Check HSQL DB by opening it in standalone mode and with URL

jdbc:hsqldb:file:D:\JavaProj\TmpDBLocation\myKauDB

You should see your changes persisted in DB.

Closing sessionfactory worked for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!