2013-11-25 50 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

相關問題