我们正在使用 HSQL数据库引擎2.3.2 和Spring 4.1.0.RELEASE
,而我的弹簧配置如下:
以下是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
操作的任何方法吗?
答案 0 :(得分:1)
这是我的错误假设因为我在applicationContext.xml
bean上hsqldb
声明了属性,如下所示:
<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=.';