gpt4 book ai didi

java - 项目重建后 Hibernate H2 数据库锁定

转载 作者:太空宇宙 更新时间:2023-11-04 11:17:20 25 4
gpt4 key购买 nike

我正在设置 Spring hibernate H2 应用程序。当服务器启动时,一切正常,但在更新后(在 Eclipse 中重建项目而不重新启动 Tomcat),我收到以下错误消息,指出无法访问数据库文件。

错误消息:

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:

在谷歌上搜索此错误后,我尝试将 File_LOCK=NODB_CLOSE_ON_EXIT=TRUE 添加到 URL,但没有成功。

Context.xml 文件

<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>

我该怎么做才能使数据库在每次重建项目时都不会被锁定。

此外,每次更新数据库后,Eclipse 都会重建应用程序。我怎样才能阻止这个?

最佳答案

尝试 DB_CLOSE_ON_EXIT=FALSE,来自 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.

关于java - 项目重建后 Hibernate H2 数据库锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45310487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com