I am working on a Maven Spring Boot web application using Hibernate for an embedded H2 database. The application is deployed to the Tomcat 8 application container using the Maven tomcat7 target: relocate from the Maven Tomcat plugin (tomcat7-maven-plugin).
When I first try to deploy this web application to Tomcat, I have no exception (after restarting Tomcat).
But when I try to remake this web application on Tomcat, I have the following exception:
org.h2.jdbc.JdbcSQLException: the database is already in use: "Blocked in another way." Possible solutions: close all other connections (s); Use 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>
<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"/>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<property name="show_sql">true</property>
<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 ;
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();
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 ;
}
[...]
}
I read on other topics that database connections close automatically when the virtual machine exits correctly (source: What is the correct way to close H2? )
I assumed that as the application is deployed to Tomcat, database connections are stored by Tomcat.
I would like to find the right way to close all database connections when redistributing Tomcat.
Thanks in advance.