每次战争部署仅创建一次hsqldb数据库

时间:2017-01-20 05:38:08

标签: java spring hsqldb

我需要将Spring应用程序数据库从MySql迁移到HSQL数据库。我在我的xml文件中配置了hsql db详细信息,并且能够运行sql文件并成功连接到它。听到问题是当我重新启动我的tomcat服务器时,新数据库正在创建并且旧数据库正在被删除。我正在查看我存储在表格中的所有数据。如何在战争部署时仅为HSQL创建一次db,或者每次重启tomcat时只创建一次。

听到我的配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/jdbc
       http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd">


  <jdbc:embedded-database id="dbcpDataSource" type="HSQL">
      <jdbc:script location="classpath:schema.sql"/>
 </jdbc:embedded-database> 

<tx:annotation-driven/>
      <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
        <property name="url" value="jdbc:hsqldb:mem:test"/> 
        <property name="username" value="sa"/>
        <property name="password" value=""/>
        <property name="maxActive" value="100000"/>
        <property name="maxIdle" value="30"/>
        <property name="maxWait" value="16000"/>
        <property name="minIdle" value="0"/>
</bean>    
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
         <property name="dataSource" ref="dbcpDataSource"/>
         <property name="packagesToScan" value="com.design.model"/>
          <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
            </props>
         </property>
        <property name="exposeTransactionAwareSessionFactory"><value>false</value></property>

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

1 个答案:

答案 0 :(得分:0)

在Web应用程序外部使用HSQLDB服务器,其中包含名为memdb的mem:数据库。请参阅http://hsqldb.org/doc/guide/listeners-chapt.html#lsc_server_props并使用<property name="url" value="jdbc:hsqldb:hsql://localhost/memdb"/>连接到此服务器。数据一直保持到关闭外部服务器为止。

相关问题