我正在使用嵌入式H2数据库上的Hibernate开发Maven Spring Boot Web应用程序.应用程序部署在Tomcat 8应用程序容器上,使用Maven目标tomcat7:从Maven Tomcat插件重新部署(tomcat7-maven-plugin).当我第一次尝试在Tomc...
我正在使用嵌入式H2数据库上的Hibernate开发Maven Spring Boot Web应用程序.
应用程序部署在Tomcat 8应用程序容器上,使用Maven目标tomcat7:从Maven Tomcat插件重新部署(tomcat7-maven-plugin).
当我第一次尝试在Tomcat上部署这个Web应用程序时,我没有异常(在Tomcat重启之后).
但是当我尝试在Tomcat上重新部署此Web应用程序时,我有以下异常:
org.h2.jdbc.JdbcSQLException: Database may be already in use: “Locked
by another process”. Possible solutions: close all other
connection(s); use the server mode; SQL statement:
null/14cfb969fb93251ff134953c65dd1f05db2ecd34c6b [90020-145]
的hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:file:d:/Profiles/mBaye/Developement/Run/spring-boot-web-seed-dev/db/springbootwebui;DB_CLOSE_DELAY=0;MVCC=TRUE</property>
<property name="connection.username">sa</property>
<property name="connection.password"/>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property> -->
<!-- Update the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="app/Greeting.hbm.xml"/>
</session-factory>
</hibernate-configuration>
GreetingController.java
@Controller
public class GreetingController {
private static Logger logger ;
// A SessionFactory is set up once for an application
private static final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
[...]
private Greeting saveGreeting(Greeting greeting) {
logger.info(new StringBuilder("greeting=").append(greeting.toString()).toString());
Session session = null;
Greeting ret = null;
try {
session = sessionFactory.openSession();
session.beginTransaction();
session.save( greeting );
session.getTransaction().commit();
// Return result
ret = greeting ;
} catch (Exception e) {
logger.log(Level.SEVERE, new StringBuilder("Failed to save ").append(greeting.toString()).toString(), e);
} finally {
session.close();
}
if (ret != null) {
logger.info(new StringBuilder("ret=").append(ret.toString()).toString());
} else {
logger.info(new StringBuilder("ret=null").toString());
}
return ret ;
}
[...]
}
我在其他主题上读到,当VM正常退出时,数据库连接会自动关闭
(来源:What is the proper way to close H2?)
我想,当应用程序部署在Tomcat上时,Tomcat会保留数据库连接.
我想找到一种正确的方法来关闭Tomcat重新部署的所有数据库连接.
提前致谢.
解决方法:
我终于找到了解决方案!
本文标题为:java – 嵌入式H2的Tomcat WebApp:数据库可能已在使用中:“被另一个进程锁定”
基础教程推荐
- Java正则校验密码至少包含字母数字特殊符号中的2种实例代码 2023-03-30
- mybatis-plus 新增/修改如何实现自动填充指定字段 2023-02-05
- Java实现map转换成json的方法详解 2022-11-16
- mybatis collection和association的区别解析 2023-03-06
- Java设计模式之装饰器模式 2023-06-02
- 以Java Web项目为例浅谈前后端分离开发模式 2022-09-03
- Spring原生Rpc六种的正确打开方式实现示例 2022-11-05
- JSP监听器用法分析 2023-08-02
- 详解如何在SpringBoot项目中使用统一返回结果 2023-06-17
- java – 使用MS SQL的JAX-RS中的行级安全性 2023-11-03