1

I am working with a file based hsql database in one of my projects. Right now we are testing a lot to ensure that the frontend works correctly. But due to the nature of our data and that there is no function to delete entire datasets I have to restart quite often to get rid of my test data in my database so that I can start some tests anew. To make it clear I am not talking about JUnit tests, there we reset our database after each start of JUnit tests. I talk about tests in the application itself. It is a JSF application by the way, but that shouldn't be too important for my question.

I was thinking about an action to reset the database during runtime. Something like drop the entire database and reinitialize it with the scripts we provide on server start.

I have found this post: https://stackoverflow.com/a/9918473/1337295

But unfortunately that doesn't work, some tokens in the query seem to be unknown for the database.

We configured our database this way:

<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:file:testdb;shutdown=true"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>

<jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="classpath:embeddedDB/embeddedDbStructure.sql"/>
    <jdbc:script location="classpath:embeddedDB/embeddedDbData.sql"/>
</jdbc:initialize-database>

We use a file based database, so that we can use the database manager swing ui, which didn't seem to work with a in-memory database.

So, is it possible to reinit the database during runtime? What would be the preferred way to do so?

Thanks in advance.

Community
  • 1
  • 1

1 Answers1

0

I would say the best way to do this is let hibernate build you a fresh database each time from it's entities by using

 <property name="hbm2ddl.auto" value="create"/>

If necessary use DBUnit to set up default data.

Is there a reason it's a file database and not in memory, if there is a lot of resetting going on?

Essex Boy
  • 7,565
  • 2
  • 21
  • 24
  • Well, as I said, we are using the database manager swing ui. I didn't get it to work with an in memory database, file based works nice and I didn't saw any difference in performance.It's also used only in our development environment, so it doesn't really matter. But how does your suggestion work in my case? Like I've said, no Unit tests here, or rather no problem with unit tests. I want to reinit the db during runtime from my application, so I guess it should rather be a programmatical solution. The data get's restored when I restart the server, I just want to avoid that step because I'm lazy – Matthias Nicklisch Jan 13 '16 at 09:36
  • Ok sorry I was focused on units tests. I would say you just use the sql script which reinitalizes the db and call that directly from a jdbc comand. Does really matter what db your using at this point. The spring embedded db support is more for set up. – Essex Boy Jan 14 '16 at 07:04