如何通过 Spring 使用脚本初始化内存中的 HSQLDB
我正在尝试对我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您尝试使用内存数据库和 Spring,有一个新的
jdbc
Spring 3 命名空间,使使用嵌入式数据库变得非常容易。最好的部分是它充当
DataSource
,因此可以轻松地将其替换为现有的dataSource
bean。如果您对使用 Java Config 执行此操作更感兴趣,请查看 EmbeddedDatabaseBuilder(Spring 3.0 中的新增功能)。
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 existingdataSource
bean.If you are more interested in doing this with Java Config, take a look at the EmbeddedDatabaseBuilder (new in Spring 3.0).
Nicholas 的回答非常好,但是您也可以使用 jdbc 命名空间来初始化外部数据库:
Nicholas answer is perfectly fine, but you can use
jdbc
namespace to initialize external database as well:在您链接到的教程中,设置方法之一是这样的(经过明显的更正后):
我认为这似乎是相关的。我建议将
path-to-file
替换为看起来像完全限定文件名的内容......In the tutorial you link to, one of the ways of setting things up is this (after obvious correction):
I think that that would appear to be relevant. I suggest replacing
path-to-file
with something that looks like a fully-qualified filename…您可以通过创建 < 的子类来解决此问题code>BasicDataSource 带有两个新属性的 getters/setters,
initExecuteSqlFile
和destroyExecuteSqlFile
,它们可以有要执行的以逗号分隔的 SQL 文件列表。子类将具有处理 init/destroy SQL 文件的init()
和destroy()
方法。然后使用以下 bean 定义:
You could get around this by creating a subclass of
BasicDataSource
with getters/setters for two new properties,initExecuteSqlFile
anddestroyExecuteSqlFile
, that can have a comma-seperated list of SQL files to execute. The subclass would haveinit()
anddestroy()
methods that handle the init/destroy SQL files.Then use the following bean definition:
使用嵌入式数据库,我们只能从同一个 JVM 连接到数据库。如果我们有两个 JVM,出于性能或其他限制,我们可以:
您可以使用 此答案。
然后像 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:
Instead of using an embedded-database, you can use the datasource suggested in this answer.
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).