2015-04-17 38 views
0

我們使用HSQL數據庫引擎2.3.2Spring 4.1.0.RELEASE,而我的春天配置如下:如何動態地在春季進行HSQLDB SET操作

這裏是applicationContext.xml

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

    <bean class="com.chorke.spring.bootstarp.HyperSqlDbServer" id="hsqldb" init-method="start"> 
     <constructor-arg> 
      <props> 
       <prop key="server.port">9002</prop> 
       <prop key="server.dbname.0">chorke</prop> 
       <prop key="server.remote_open">true</prop> 
       <prop key="server.database.0">file:~/.hsqldb/chorke/data;sql.syntax_mys=true;user=admin;password=pa55word</prop> 
       <prop key="hsqldb.default_table_type">text</prop> 
       <prop key="hsqldb.reconfig_logging">false</prop> 
      </props> 
     </constructor-arg> 
    </bean> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver" /> 
     <property name="url" value="jdbc:hsqldb:hsql://localhost:9002/chorke" /> 
     <property name="username" value="admin" /> 
     <property name="password" value="pa55word" /> 
    </bean> 

    <jdbc:initialize-database data-source="dataSource"> 
     <jdbc:script location="classpath:schema.sql"/> 
     <jdbc:script location="classpath:data.sql"/> 
    </jdbc:initialize-database> 

</beans> 

這裏是com.chorke.spring.bootstarp.HyperSqlDbServer

package com.chorke.spring.bootstarp; 

import java.io.IOException; 
import java.util.Properties; 

import org.hsqldb.Server; 
import org.hsqldb.persist.HsqlProperties; 
import org.hsqldb.server.ServerAcl.AclFormatException; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.context.SmartLifecycle; 

public class HyperSqlDbServer implements SmartLifecycle { 
    private final Logger logger = LoggerFactory.getLogger(HyperSqlDbServer.class); 
    private HsqlProperties properties; 
    private Server server; 
    private boolean running = false; 

    public HyperSqlDbServer(Properties props) { 
     properties = new HsqlProperties(props); 
    } 

    @Override 
    public boolean isRunning() { 
     if (server != null) 
      server.checkRunning(running); 
     return running; 
    } 

    @Override 
    public void start() { 
     if (server == null) { 
      logger.info("Starting HSQL server..."); 
      server = new Server(); 
      try { 
       server.setProperties(properties); 
       server.start(); 
       running = true; 
      } catch (AclFormatException afe) { 
       logger.error("Error starting HSQL server.", afe); 
      } catch (IOException e) { 
       logger.error("Error starting HSQL server.", e); 
      } 
     } 
    } 

    @Override 
    public void stop() { 
     logger.info("Stopping HSQL server..."); 
     if (server != null) { 
      server.stop(); 
      running = false; 
     } 
    } 

    @Override 
    public int getPhase() { 
     return 0; 
    } 

    @Override 
    public boolean isAutoStartup() { 
     return true; 
    } 

    @Override 
    public void stop(Runnable runnable) { 
     stop(); 
     runnable.run(); 
    } 
} 

這裏是schema.sql

CREATE TABLE IF NOT EXISTS t001i001(pf_id INTEGER,df_name VARCHAR(20)); 
--SET TABLE t001i001 SOURCE 't001i001.csv;fs=|;vs=.'; 

這裏是data.sql

insert into t001i001(pf_id, df_name) values(1, 'Shefat Hossain'); 

,我想補充一些腳本來執行動態一些SET操作等作爲什麼在schema.sql評論第二行。使用春天。


任何方式在Spring應用程序上下文中邏輯/動態執行SET操作?

回答

1

這裏是我的錯誤假設,因爲當我宣佈hsqldb豆如低於applicationContext.xml屬性:

<prop key="hsqldb.default_table_type">text</prop> 

它的假設,我認爲這將是幫助我們默認的數據庫表引擎TEXT,而不是MEMORY 。由於表格默認引擎是MEMORY所以schema.sql的第二行沒有執行。編輯後schema.sql它工作正常,SET操作沒問題,如下:

CREATE TEXT TABLE IF NOT EXISTS t001i001(pf_id INTEGER,df_name VARCHAR(20)); 
SET TABLE t001i001 SOURCE 't001i001.csv;fs=|;vs=.';