3

For logging and debugging purposes I want to dump an embedded/in-memory HSQL database to a file. Schema + Data. I'm using the spring-framework with hibernate.

I've tried both:

    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();

    session.createSQLQuery("BACKUP DATABASE TO '/tmp/backup.tar.gz' BLOCKING");

    transaction.commit();
    session.close();

and

    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();

    session.createSQLQuery("SCRIPT '/tmp/backup-data.sql'");

    transaction.commit();
    session.close();

Both to no avail.

There is no special configuration.

Hibernate config:

<jdbc:embedded-database id="dataSource" type="HSQL">
    <jdbc:script location="classpath:spring/batch/database/schema.sql"/>
</jdbc:embedded-database>

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
      p:dataSource-ref="dataSource">
    <property name="packagesToScan" value="com.domain.*.model"/>
    <property name="hibernateProperties">
        <value>
            hibernate.format_sql=true
            hibernate.dialect=org.hibernate.dialect.HSQLDialect
        </value>
    </property>
</bean>

What did I miss, do I need a different approach, is it even possible?

1
  • You are only creating the query, not executing it. Commented Nov 26, 2013 at 10:16

2 Answers 2

3

Your code is only creating an instance of a SQLQuery and throws it away. You aren't executing the query.

Add/change to the following

SQLQuery query = session.createSQLQuery("BACKUP DATABASE TO '/tmp/backup.tar.gz' BLOCKING");
query.executeUpdate();

This will execute the query.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried:

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

session.createSQLQuery("BACKUP DATABASE TO '/tmp/' BLOCKING");

transaction.commit();
session.close();

According to the documentation you need to give the directory and HSQL will create the tar.gz file in it.

1 Comment

Yes I have also tried this. And again just now to be sure ;) This does not work for me either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.