2013-11-25 54 views
1

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

一句话......

访问存储器中H2,即 “的jdbc:H2:MEM:MYAPP”,通过Web控制台doesn't工作。

通过“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> 

回答

3

挖一点点深入H2文档,我发现少了什么:

当使用内存数据库,默认行为是,只要上次连接关闭,它就会自动关闭。我的其他应用程序(控制台刚开箱时工作得很好)使用了一个c3p0连接池,它始终保持一些连接打开 - >此行为从不发生。

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

为了防止出现这种情况,您可以简单地将DB_CLOSE_DELAY = -1添加到jdbcUrl的末尾。这样,只要jvm就可以继续运行数据库。

我JDBCURL现在看起来

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

对于那些有兴趣在该文档中,看到h2 docs

希望,我可以帮助别人。

+0

感谢buddy..It救了我的一天! – Dhairyashil

相关问题