如何通过 Spring 使用脚本初始化内存中的 HSQLDB

发布于 2025-01-06 18:53:26 字数 896 浏览 5 评论 0原文

我正在尝试对我的 DAO 进行单元测试(使用 Spring 和 Hibernate)。我按照教程使用HSQLDB。该教程指出可以使用 SQL 脚本初始化内存中的 HSQLDB 数据库,但我找不到有关如何在 Spring 中执行此操作的信息。这是相关的 Spring 上下文配置:

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:mydb" />
    <property name="username" value="sa" />
    <property name="password" value="" />
    <property name="initialSize" value="5" />
    <property name="maxActive" value="10" />
    <property name="poolPreparedStatements" value="true" />
    <property name="maxOpenPreparedStatements" value="10" />
</bean> 

任何帮助将不胜感激。谢谢。

I am attempting to do unit testing of my DAO (using Spring and Hibernate). I am using HSQLDB per this tutorial. The tutorial states that the in-memory HSQLDB database can be initialized using a SQL script but I cannot find information on how to do so in Spring. Here is the pertinent Spring context config:

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:mydb" />
    <property name="username" value="sa" />
    <property name="password" value="" />
    <property name="initialSize" value="5" />
    <property name="maxActive" value="10" />
    <property name="poolPreparedStatements" value="true" />
    <property name="maxOpenPreparedStatements" value="10" />
</bean> 

Any help would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

-黛色若梦 2025-01-13 18:53:27

如果您尝试使用内存数据库和 Spring,有一个新的 jdbc Spring 3 命名空间,使使用嵌入式数据库变得非常容易。

最好的部分是它充当 DataSource,因此可以轻松地将其替换为现有的 dataSource bean。

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

如果您对使用 Java Config 执行此操作更感兴趣,请查看 EmbeddedDatabaseBuilder(Spring 3.0 中的新增功能)。

@Configuration
public class DatabaseTestConfig {
    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.HSQL)
            .addScript("classpath:schema.sql")
            .addScript("classpath:test-data.sql")
            .build();
    }
}

If you are trying to work with in-memory databases and Spring, there is a new jdbc namespace for Spring 3 that makes working with embedded databases very easy.

The best part is that it acts as a DataSource, so it can easily be dropped in to replace your existing dataSource bean.

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

If you are more interested in doing this with Java Config, take a look at the EmbeddedDatabaseBuilder (new in Spring 3.0).

@Configuration
public class DatabaseTestConfig {
    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
            .setType(EmbeddedDatabaseType.HSQL)
            .addScript("classpath:schema.sql")
            .addScript("classpath:test-data.sql")
            .build();
    }
}
野却迷人 2025-01-13 18:53:27

Nicholas 的回答非常好,但是您也可以使用 jdbc 命名空间来初始化外部数据库:

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/DS"/>

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

Nicholas answer is perfectly fine, but you can use jdbc namespace to initialize external database as well:

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/DS"/>

<jdbc:initialize-database data-source="dataSource">
    <jdbc:script location="classpath:/META-INF/database/init.sql"/>
</jdbc:initialize-database>
瞳孔里扚悲伤 2025-01-13 18:53:27

在您链接到的教程中,设置方法之一是这样的(经过明显的更正后):

  • 来自脚本的内存中:jdbc:hsqldb:file:path-to-file

我认为这似乎是相关的。我建议将 path-to-file 替换为看起来像完全限定文件名的内容......

In the tutorial you link to, one of the ways of setting things up is this (after obvious correction):

  • In-memory from a script: jdbc:hsqldb:file:path-to-file

I think that that would appear to be relevant. I suggest replacing path-to-file with something that looks like a fully-qualified filename…

不顾 2025-01-13 18:53:27

您可以通过创建 < 的子类来解决此问题code>BasicDataSource 带有两个新属性的 getters/setters,initExecuteSqlFiledestroyExecuteSqlFile,它们可以有要执行的以逗号分隔的 SQL 文件列表。子类将具有处理 init/destroy SQL 文件的 init()destroy() 方法。

然后使用以下 bean 定义:

<bean
    id="datasource"
    class="com.example.MyBasicDataSource"
    destroy-method="destroy"
    init-method="init"
>
    <property name="destroyExecuteSqlFile">
        <value>h2-destroy-01.sql</value>
    </property>
    <property name="initExecuteSqlFile">
        <value>h2-init-01.sql,h2-init-02.sql,h2-init-03.sql</value>
    </property>
    <!-- Other properties -->
</bean>

You could get around this by creating a subclass of BasicDataSource with getters/setters for two new properties, initExecuteSqlFile and destroyExecuteSqlFile, that can have a comma-seperated list of SQL files to execute. The subclass would have init() and destroy() methods that handle the init/destroy SQL files.

Then use the following bean definition:

<bean
    id="datasource"
    class="com.example.MyBasicDataSource"
    destroy-method="destroy"
    init-method="init"
>
    <property name="destroyExecuteSqlFile">
        <value>h2-destroy-01.sql</value>
    </property>
    <property name="initExecuteSqlFile">
        <value>h2-init-01.sql,h2-init-02.sql,h2-init-03.sql</value>
    </property>
    <!-- Other properties -->
</bean>
风筝在阴天搁浅。 2025-01-13 18:53:27

使用嵌入式数据库,我们只能从同一个 JVM 连接到数据库。如果我们有两个 JVM,出于性能或其他限制,我们可以:

  1. 您可以使用 此答案

  2. 然后像 Poitrek De 建议的那样进行初始化(并在 之前的答案)。您可能只想在表不存在时创建表(如建议的此处)。

With embedded-database we would only be able to connect to the database from the same JVM. If we have two JVMs, for performance or other constraints, we can:

  1. Instead of using an embedded-database, you can use the datasource suggested in this answer.

  2. Then initialize like Poitrek De suggested (and suggested in previous answer too). You may want to create tables only if they do not exist (as suggested here).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文