25

Something of an novice with HSQL and Hibernate...

em.getTransaction().begin();
for (Activity theActivity : activities) {
  em.persist(theActivity);
}
em.getTransaction().commit();
em.close();

followed by...

EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
System.out.println("QUERY:: "
    + em.createQuery("SELECT COUNT(*) FROM " + Activity.class.getName()).getSingleResult()
        .toString());
em.getTransaction().commit();

Prints 25000 (the number of Activity objects in activities). But when I run this test again, the number of objects in the count(*) doesn't increase (and is 0 at the beginning of the program). So the objects aren't getting durably written.

This is my hsqldb connection string:

name="hibernate.connection.url" value="jdbc:hsqldb:file:data/cmon"

so it's not an in-memory database as far as I know...

Does anyone have any ideas why the objects aren't getting persisted beyond a single JVM session? Happy to supply more information but there's so much state associated with Hibernate / JPA / HSQL that it's not clear exactly what is pertinent.

Pascal Thivent
  • 535,937
  • 127
  • 1,027
  • 1,106
HenryR
  • 7,261
  • 6
  • 31
  • 38
  • As another data point, the last run's INSERTs are in data/cmon.log, which is erased on startup and replaced. Maybe there's an option to force log replay at startup? – HenryR Aug 23 '10 at 23:35
  • This means logging does take place. Please report the size of the .log and of any .script file in that directory. Also the version of HSQLDB you are using. – fredt Aug 23 '10 at 23:48
  • @fredt - Hibernate 1.8.0. The size of the log is about 4MB, around 37000 insert statements. cmon.script is 1.9K, and curiously ends with SET WRITE_DELAY 10 - which makes it look like my changes to the connection string aren't working. – HenryR Aug 24 '10 at 17:51
  • The connection property is not supported by HSQLDB 1.8.0. Latest Hibernate 3.5.5 (2010.08.18) release supports HSQLDB 2.0.x. Get this together with the latest HSQLDB 2.0.1 snapshot jar from http://hsqldb.org/support/ and use jdbc:hsqldb:file:data/cmon;hsqldb.write_delay=true;shutdown=true – fredt Aug 24 '10 at 23:02
  • Correction: jdbc:hsqldb:file:data/cmon;hsqldb.write_delay=false;shutdown=true – fredt Aug 25 '10 at 00:47
  • 1
    @HenryR: Did you ever get it to work? I am having the exact same problem. Logs get written, but no data. – oligofren Mar 15 '11 at 14:33
  • I am having a similar problem. The persists seem to be getting through but not all of them. – James P. Aug 21 '11 at 19:52

6 Answers6

30

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
Pascal Thivent
  • 535,937
  • 127
  • 1,027
  • 1,106
  • Thanks, I'll try that. So committing a transaction doesn't guarantee persistence, it seems - that's a bit counter intuitive. – HenryR Aug 23 '10 at 22:48
  • @HenryR: That's exactly it, HSQLDB is not Durable **by default**. (actually, HSQLDB is not A(C)ID). – Pascal Thivent Aug 23 '10 at 22:58
  • @Pascal Thivent - thanks, unfortunately that doesn't seem to have worked. My connection string is jdbc:hsqldb:file:data/cmon;hsqldb.write_delay=0;shutdown=true and there is still no durability (hbm2ddl.auto is still set to 'update') – HenryR Aug 23 '10 at 23:33
  • @HenryR: I added a link to a similar question (that I couldn't find back when I wrote my answer). I believe it's the same "problem". Will test this tomorrow. – Pascal Thivent Aug 23 '10 at 23:56
  • HSQLDB version 1.8.x does not support the connection property hsqldb.write_delay at all (only the SQL command is supported). Version 2.0.x supports hsqldb.write_delay=false (the SQL command is also slightly different) – fredt Aug 24 '10 at 00:09
  • @fredt: Thanks, I've clarified this in my answer. – Pascal Thivent Aug 24 '10 at 03:16
  • NB hsqldb.write_delay=0 is not valid, only true or false. – fredt Aug 24 '10 at 07:02
  • 1
    Invalid Edit by anonymous user: Finally closing the EntityManagerFactory (emf.close()) before the application quits also flushes the changes to disk: EntityManagerFactory emf; //set up emf //persist some entities emf.close(); – StuartLC Oct 28 '12 at 10:11
9

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>
Maciek Kreft
  • 854
  • 8
  • 14
8

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.

Chris Lercher
  • 36,020
  • 19
  • 96
  • 128
4

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.

Kaushik Lele
  • 5,681
  • 9
  • 44
  • 68
3

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

Jean-Luc
  • 31
  • 1
1

Closing sessionfactory worked for me.

Pradeep
  • 87
  • 1
  • 8