2017-01-20 41 views
0

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

听到的是我的配置文件:

<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> 

回答

0

使用的HSQLDB服务器的Web应用程序之外用MEM:数据库命名为memdb。请参阅http://hsqldb.org/doc/guide/listeners-chapt.html#lsc_server_props并通过<property name="url" value="jdbc:hsqldb:hsql://localhost/memdb"/>连接到此服务器。数据一直保留到关闭外部服务器。

+0

我甚至使用内存配置要么,我的要求是当外部服务器重新启动,那么我的数据库也不应该创建。它只需要创建一次(即我们第一次部署战争) – Navyah

+0

在这种情况下,在服务器中使用'file:'数据库来保存数据。 – fredt

+0

即使在使用文件后,它也会提供相同的问题。 Navyah