【问题标题】:Cannot see content of h2 database when using it "in-memory"使用“内存”时看不到 h2 数据库的内容
【发布时间】:2013-11-25 17:52:24
【问题描述】:

我目前正在开发一个 spring mvc 应用程序,我的域模型通过 hibernate (4) 映射到 h2 数据库。我当前的问题是,当我将 h2 网络服务器作为 bean 启动时(参见下面的上下文),当它通过数据源的 jdbcUrl 创建“内存中”时,我看不到数据库。将 jdbcUrl 设置为一个文件,然后通过 Web 控制台访问它 -> 一切正常!

一句话……

无法通过 webconsole 访问内存中的 h2,即“jdbc:h2:mem:myapp”。

通过“jdbc:h2:file:/tmp/myapp”访问它就可以了。

请注意,问题不在于通过休眠等方式保存条目...我只是没有通过 h2 的 Web 控制台看到内存数据库。我也尝试过像这个线程 (link) 中提到的那样启动一个 tcp 服务器,但它并没有解决我的问题。

有什么想法吗?

这是我的 servlet-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <context:annotation-config />

    <context:component-scan base-package="org.wt.myapp" />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

<!--    Database   -->

    <beans:bean id="h2console" class="org.h2.tools.Server"
        lazy-init="false" factory-method="createWebServer" init-method="start" >
        <beans:constructor-arg value="-web,-webAllowOthers,-webPort,8082" />
    </beans:bean>

    <beans:bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <beans:property name="username" value="" />
        <beans:property name="password" value="" />
        <beans:property name="driverClassName" value="org.h2.Driver" />
        <beans:property name="url" value="jdbc:h2:mem:myapp" />
    </beans:bean>

    <beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
       <beans:property name="dataSource" ref="dataSource" />
       <beans:property name="packagesToScan" value="org.wt.myapp.db.domain" />
       <beans:property name="hibernateProperties">
           <beans:props>
               <beans:prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</beans:prop>
               <beans:prop key="hibernate.show_sql">true</beans:prop>
               <beans:prop key="hibernate.hbm2ddl.auto">create-drop</beans:prop>
           </beans:props>
       </beans:property>
    </beans:bean>

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

    <tx:annotation-driven/>

</beans:beans>

【问题讨论】:

    标签: database spring hibernate h2


    【解决方案1】:

    深入研究 h2 文档,我发现了缺失的内容:

    使用内存数据库时,默认行为是在最后一个连接关闭后立即自动关闭。我的另一个应用程序(控制台几乎开箱即用)使用了一个 c3p0 连接池,它始终保持一些连接打开 -> 这种行为永远不会发生。

    在问题(和我当前的应用程序)中,有一个 DriverManagerDataSource 不会一直保持连接打开......所以,当休眠准备好并完成创建架构时,数据库会关闭。

    要防止这种行为,您只需在 jdbcUrl 末尾添加 DB_CLOSE_DELAY=-1。这样,只要 jvm 运行,数据库就会一直运行。

    我的 jdbcUrl 现在看起来像

    jdbc:h2:mem:myapp;DB_CLOSE_DELAY=-1
    

    对文档感兴趣的人,请参阅h2 docs

    希望,我可以帮助别人。

    【讨论】:

    • 谢谢哥们..它拯救了我的一天!
    猜你喜欢
    • 2023-03-24
    • 2019-12-07
    • 2011-08-11
    • 2016-06-08
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    相关资源
    最近更新 更多