tomcatwebapp with embedded h2:数据库可能已经在使用中:“被另一个进程锁定”

b4lqfgs4  于 2021-07-13  发布在  Java
关注(0)|答案(3)|浏览(234)

我正在使用嵌入式h2数据库上的hibernate开发maven-spring-boot web应用程序。应用程序使用maven-goal-tomcat7:redeployfrom-maven-tomcat-plugin(tomcat7-maven-plugin)部署在tomcat8应用程序容器上。
当我第一次尝试在tomcat上部署这个web应用程序时,我没有例外(在tomcat重启之后)。
但当我尝试在tomcat上重新部署此web应用程序时,有以下例外:
org.h2.jdbc.jdbcsqlexception:数据库可能已在使用:“被另一个进程锁定”。可能的解决方案:关闭所有其他连接;使用服务器模式;sql语句:null/14cfb969fb93251ff134953c65dd1f05db2ecd34c6b[90020-145]
休眠.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>

欢迎控制器.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正确退出时,数据库连接会自动关闭(来源:关闭h2的正确方法是什么?)
我假设,由于应用程序部署在tomcat上,数据库连接由tomcat持有。
我想找到一个合适的方法来关闭tomcat上的所有数据库连接。
提前谢谢。

brtdzjyr

brtdzjyr1#

我终于找到了解决办法!:)
我将h2数据库连接url的设置更改为:

<property name="connection.url">jdbc:h2:file:d:/Profiles/mBaye/Developement/Run/spring-boot-web-seed-dev/db/springbootwebui;MVCC=TRUE;DB_CLOSE_ON_EXIT=TRUE;FILE_LOCK=NO</property>

休眠.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;MVCC=TRUE;DB_CLOSE_ON_EXIT=TRUE;FILE_LOCK=NO</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>

我不确定这是不是最好的解决办法,但它是有效的。

hjzp0vay

hjzp0vay2#

h2在关闭与数据库的所有连接时关闭数据库。关闭connectionpool中的所有连接对我有效。

6uxekuva

6uxekuva3#

关闭连接也为我工作,有多个连接打开而没有意识到。

相关问题