My application is using an in memory h2 database with Hibernate, I was noticing that even after closing the application the h2 lock file remained. This occurred even if I started the application and then immediately without doing anything much closed the application.
Further investigation revealed that when I first start the application I make a test connection to check the database is okay as follows (exiting application if I can not connect)
try
{
Session session = HibernateUtil.getSession();
Transaction t = session.getTransaction();
t.setTimeout(10);
session.beginTransaction();
t.commit();
HibernateUtil.closeSession(session);
}
catch (Exception ex)
{
MainWindow.logger.log(Level.SEVERE, "Problem accessing database needs recreating:" + ex.getMessage(), ex);
System.exit(0);
}
removing this code solves the problem with the lock file, it now disappears. But I don't understand why it doesn't disappear anyway as I am closing the session. And the problem doesn't go away because if I do any real work in the application then once again I'm left with the problem of the lock file being left behind.
HibernateUtil methods are
public static Session getSession()
{
if (factory == null)
{
Configuration config =
HibernateUtil.getInitializedConfiguration();
factory = config.buildSessionFactory();
}
Session hibernateSession =
factory.openSession();
return hibernateSession;
}
public static void closeSession(Session session)
{
if(session!=null)
{
session.close();
}
}
public static Configuration getInitializedConfiguration()
{
Configuration config = new Configuration();
config.setProperty(Environment.DRIVER,"org.h2.Driver");
config.setProperty(Environment.URL,"jdbc:h2:"+Db.DBFOLDER+"/"+Db.DBNAME+";FILE_LOCK=SOCKET;MVCC=TRUE;DB_CLOSE_ON_EXIT=FALSE;CACHE_SIZE=50000");
config.setProperty(Environment.DIALECT,"org.hibernate.dialect.H2Dialect");
org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider");
config.setProperty("hibernate.connection.username","jaikoz");
config.setProperty("hibernate.connection.password","jaikoz");
config.setProperty("hibernate.c3p0.numHelperThreads","10");
config.setProperty("hibernate.c3p0.min_size","20");
config.setProperty("hibernate.c3p0.max_size","100");
config.setProperty("hibernate.c3p0.timeout","300");
config.setProperty("hibernate.c3p0.maxStatementsPerConnection","50");
config.setProperty("hibernate.c3p0.idle_test_period","3000");
config.setProperty("hibernate.c3p0.acquireRetryAttempts","10");
config.setProperty("hibernate.show_sql","false");
addEntitiesToConfig(config);
return config;
}
My url connection is of the form
jdbc:h2:Database/Database.h2.db;FILE_LOCK=SOCKET;MVCC=TRUE;DB_CLOSE_ON_EXIT=FALSE;CACHE_SIZE=50000
I tried changing it to
jdbc:h2:Database/Database.h2.db;FILE_LOCK=SOCKET;MVCC=TRUE;DB_CLOSE_ON_EXIT=TRUE;CACHE_SIZE=50000
but that had no effect
The reason why this is a real problem for me is that I have a recreateDatabase() command that the users can use, and this physicially deletes the database files before they are recreated, but of course they cannot be deleted if the files are still being used, as they are as evidenced by the existence of the lock file. So the recreateDatabase() command doesnt work