Search code examples
javahibernatehsqldb

hibernate/hsqldb: Database file not found after shutdown


When I close hibernate my data connection, there is still a lock file present.

This is what I have tried:

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class TestEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;

    @Column public String name = "";

    public TestEntity() { }
}

public class TestWhyNotUnlockDatabase {

    public static void main(String[] args) throws Exception {

            Map<String, String> connProp =      new HashMap<String,String>();
            connProp.put("javax.persistence.jdbc.url","jdbc:hsqldb:file:\\Users\\Bernd\\.mld\\0.2\\solala.db;shutdown=true");
            connProp.put("javax.persistence.schema-generation.database.action", "none");

            System.out.println("Creating DB");
            EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("WhyNotUnLock",connProp);
            EntityManager entityManager = entityManagerFactory.createEntityManager();

            System.out.println("persisting");
            entityManager.getTransaction().begin();
            entityManager.persist(new TestEntity());
            entityManager.getTransaction().commit();

            System.out.println("closing");
            entityManager.clear();
            entityManager.close();
            entityManagerFactory.close();
            System.out.println("DB closed");
    }

}

The output is as expected:

Creating DB
WARN   2017-02-36 12:59:29  [main] org.hibernate.orm.connections.pooling - HHH10001002: Using Hibernate built-in connection pool (not for production use!)
persisting
closing
DB closed

However only the following files are here after end the execution of the program:

solala.db.properties
solala.db.script

My questions:

  • I would have expected that the data is stored in a file called solala.db, but I cannot find this. Where is the database file?

  • Some other odd observation: I execute this in Windows Eclipse IDE (Neon.2 Release (4.6.2)). After running this programm an exit Exclipse, Eclipse cannot be started anymore. No error message is given. Only a re-install of Eclipse helps (re-boot is not sufficient). Seems that some artefacts remain after end of this programm.

Additional Infos Main part in persitence.xml:

<persistence-unit name="WhyNotUnLock"   transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>masterlist.trials.TestEntity</class>


         <exclude-unlisted-classes>true</exclude-unlisted-classes>

        <properties>
             <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
            <property name="connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />        
            <property name="javax.persistence.jdbc.user" value="SA" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:file://C:\\Users\\Bernd\\.mld\\0.1\\smallDB.db"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />

            <property name="hibernate.max_fetch_depth" value="3" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />

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

            <!-- Echo all executed SQL to stdout -->
            <property name="hibernate.show_sql" value="false" />
            <property name="javax.persistence.schema-generation.database.action" value="none"/>


        </properties>
    </persistence-unit>

and my dependencies in build.gradle:

compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.6.Final'
compile group: 'org.hibernate', name: 'hibernate-ehcache', version: '5.2.6.Final'
compile group: 'org.hibernate', name: 'hibernate-validator', version: '5.4.0.CR1'
compile 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final' 
compile group: 'org.hibernate', name: 'hibernate-c3p0', version: '5.2.6.Final' 
compile 'com.enigmabridge:hibernate4-sqlite-dialect:0.1.2'

Edit 1 (no dublicate)

It was suggested that this question is a dublicate of enter link description here. I looked at this answer. Unfortunatly I couldn't get the answer from this link. In this link the author wants an inmemory database, where all files are removed after shutdown. In contrast I want that the database file persists unlooked after app shutdown.

Edit 2 (shutdown=true )

The option shutdown=true was added to the code. Now the locking is removed. Anyhow the database file is still missing. Title of question adapted.


Solution

  • Partly answer: Setting shutdown=true removed the temp file and the lock file:

    connProp.put("javax.persistence.jdbc.url","jdbc:hsqldb:file:\\Users\\Bernd\\.mld\\0.2\\solala.db;shutdown=true");
    

    Not only two files remain:

    solala.db.properties
    solala.db.script
    

    I'm still searching for solala.db