java - h2 leaving lock file even though only create one connection and immediately disconnect -


my application using in memory h2 database hibernate, noticing after closing application h2 lock file remained. occurred if started application , without doing closed application.

further investigation revealed when first start application make test connection check database okay follows (exiting application if 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 code solves problem lock file, disappears. don't understand why doesn't disappear anyway closing session. , problem doesn't go away because if real work in application once again i'm left problem of 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 of form

jdbc:h2:database/database.h2.db;file_lock=socket;mvcc=true;db_close_on_exit=false;cache_size=50000 

i tried changing

jdbc:h2:database/database.h2.db;file_lock=socket;mvcc=true;db_close_on_exit=true;cache_size=50000 

but had no effect

the reason why real problem me have recreatedatabase() command users can use, , physicially deletes database files before recreated, of course cannot deleted if files still being used, evidenced existence of lock file. recreatedatabase() command doesnt work

you're using connection pool. closing connection won't close it, return connection pool.

this means first step read documentation c3p0connectionprovider find out how talks c3p0. need access combopooleddatasource can call reset(). setting min_size 0 might help.

that said, dangerous unless have full control on threads. if don't, other thread might want access database kill connections. if web application, need install filter denies requests long reset database.

also note you're not using in-memory database; you're using embedded file-based database. create in-memory database, use jdbc:h2:mem:.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -