views:

101

answers:

3

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.

+2  A: 

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
Thanks - it's set to "update", so I'm not sure if that's the problem :(
HenryR
+4  A: 

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
Thanks, I'll try that. So committing a transaction doesn't guarantee persistence, it seems - that's a bit counter intuitive.
HenryR
@HenryR: That's exactly it, HSQLDB is not Durable **by default**. (actually, HSQLDB is not A(C)ID).
Pascal Thivent
@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
@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
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
@fredt: Thanks, I've clarified this in my answer.
Pascal Thivent
NB hsqldb.write_delay=0 is not valid, only true or false.
fredt
A: 

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