develop1 develop1 - 3 years ago 188
Java Question

Hibernate H2 database locks after project rebuild

I am setting up a Spring hibernate H2 application. When the server starts everything works but after an update (rebuild project in eclipse without restarting Tomcat) I get the following error messages saying that the database file can't be accessed.

Error Messages:

java.lang.IllegalStateException: The file is locked: nio:/home/bob/dataStore.mv.db [1.4.187/7]

java.nio.channels.OverlappingFileLockException

org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Database may be already in use:


After Googling this error I tried adding
File_LOCK=NO
and
DB_CLOSE_ON_EXIT=TRUE
to the URL but no luck.

Context.xml
file

<context:annotation-config />
<context:component-scan base-package="com" />

<mvc:annotation-driven />

<mvc:resources mapping="/resources/**" location="/resources/" />


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<!-- <property name="url" value="jdbc:h2:tcp://localhost/~/dataStore;FILE_LOCK=NO;DB_CLOSE_ON_EXIT=TRUE" /> -->
<property name="url" value="jdbc:h2:file:~/dataStore;FILE_LOCK=NO;DB_CLOSE_ON_EXIT=TRUE;MVCC=TRUE" />

<property name="username" value="" />
<property name="password" value="" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.entities" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="format_sql">true</prop>
<prop key="use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


What can I do so that the database does not get locked every time the project is rebuilt.

Also, eclipse is rebuilding the application after everytime the database is updated. How can I stop this?

Answer Source

Try DB_CLOSE_ON_EXIT=FALSE, from the Spring docs

If, for whatever reason, you do configure the connection URL for an embedded database, care should be taken to ensure that the database’s automatic shutdown is disabled. If you’re using H2 you should use DB_CLOSE_ON_EXIT=FALSE to do so.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download