3

I took care of commiting transaction, but database remains empty after saving object. No errors, hibernate even shows SQL command of inserting. Everything is ok when I add <property name="hibernate.connection.autocommit">true</property> line to my hibernate config xml, but I heard that this is not a good practice. I'm using HSQLDB database.

Here's piece of my code that inserts data.

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    Client c = new Client();
    c.setImie("John");
    c.setNazwisko("Kennedy"); 
    session.save(c);
    session.getTransaction().commit();
    session.close();
jwa
  • 3,239
  • 2
  • 23
  • 54
polmarex
  • 1,363
  • 5
  • 19
  • 35

4 Answers4

0

Some of these can happen automatically under some circumstances, but unless you are configured to do them automatically, you need to do all three of them in this order:

  1. Session.save() updates Hibernate's view of the object from inside the JVM.
  2. Session.flush() updates the database's view of the object by issuing SQL statements.
  3. Session.getTransaction().commit() commits the transaction in the database.

See http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/Session.html#flush()

John Watts
  • 8,717
  • 1
  • 31
  • 35
  • I added session.flush() before commit() as you suggested, but nothing changed - database is still empty – polmarex Jul 23 '12 at 22:28
0

You probably forgot to save your objects to the database using a session.flush() BEFORE commiting to the database (session.getTransaction().commit()). As the Hibernate reference itself states:

Force this session to flush. Must be called at the end of a unit of work, before committing the transaction and closing the session (depending on flush-mode, Transaction.commit() calls this method).

And also:

"Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory."

Edit:

After reading a little bit about HSQLDB I saw some things that could be happening to you:

1 - Because HSQLDB is not Durable by default (the D from 'ACID'), it will not persist data to the disk immediately. I know. Not so cute. So you have to "force" the write of your changes when the last connection is finished, setting the property shutdown=true; From the user guide:

You can use the shutdown=true connection property to ensure the database is persisted fully after the connections are closed.

2 - To make your changes be written immediately to disk use the property hsqldb.write_delay=false;

So, you'll end up with something like this in your connection string:

;shutdown=true;hsqldb.write_delay=false;

Hope it helps!

Tiago Farias
  • 3,397
  • 1
  • 27
  • 30
  • I changed my code as you suggested but it didn't help. No idea what's going on. – polmarex Jul 23 '12 at 22:57
  • Ok. Could you edit your post with some more detail? Maybe post your xml config as well would be good. – Tiago Farias Jul 24 '12 at 01:34
  • Sorry for such late response, but I had a little time recently and I discovered that the problem is a bit more complex than I described. And also, I couldn't figure out what you mean by 'connection string', I tried many combinations with `jdbc:hsqldb:file:/db/;shutdown=true;hsqldb.write_delay=false ` or something like that but with no effect. – polmarex Jul 27 '12 at 17:31
0

The solution to my problem is described in the first comment under question in this thread link

Community
  • 1
  • 1
polmarex
  • 1,363
  • 5
  • 19
  • 35
0

Sometimes adding ;shutdown=true to your connection string does not help when making changes through code.

So, every time you make changes (insert, delete, update, etc.) through code in the HSQLDB, just run the query "SHUTDOWN SCRIPT" manually from your code. This will shut down HSQLDB properly and will save the changes in the .properties and .script files instantly.

Your query should look like this:

SQLQuery query = session.createSQLQuery("SHUTDOWN SCRIPT");
MaxV
  • 2,601
  • 3
  • 18
  • 25
hades
  • 26
  • 3