【问题标题】:How to perform Hsqldb SET Operation dynamically in Spring如何在 Spring 中动态执行 Hsqldb SET 操作
【发布时间】:2015-06-24 13:52:36
【问题描述】:

我们使用 HSQL Database Engine 2.3.2Spring 4.1.0.RELEASE,而我的 spring 配置如下:

这里是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 Application Context 中逻辑/动态地执行SET 操作?

【问题讨论】:

    标签: spring jakarta-ee hsqldb h2 spring-jdbc


    【解决方案1】:

    这是我的错误假设,因为我在hsqldb bean 上声明了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=.';
    

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      相关资源
      最近更新 更多