@Autowired jdbcTemplate和h2内存数据库多次执行runscript [英] runscript executed multiple times for @Autowired jdbcTemplate and h2 in-memory database

查看:314
本文介绍了@Autowired jdbcTemplate和h2内存数据库多次执行runscript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一个项目,正在尝试针对内存h2数据库运行一组集成测试.为了使它们传递一些表,需要创建关系和参考数据.

I've inherited a project and am trying to get a set of integration tests running against an in-memory h2 database. In order for them to pass some tables, relationships and reference data needs creating.

我看到的问题是,在RUNSCRIPT中引用的脚本被多次执行,因此会生成Index "XXX_IDX" already exists错误和其他违规行为.那么有没有办法强制脚本只运行一次,还是我需要一个外部数据库?看来该脚本在我假定为 设计的每个连接上运行.

I can see the problem in that the script referenced in RUNSCRIPT is being executed multiple times and therefore generating Index "XXX_IDX" already exists errors and other violations. So is there a way to force the script to only be run once or do I need a external database? It seems that the script is run on every connection which I assume is by design.

属性文件

my.datasource.url=jdbc:h2:mem:my_db;DB_CLOSE_DELAY=-1;MODE=Oracle;MVCC=TRUE;INIT=RUNSCRIPT FROM 'classpath:/create-tables-and-ref-data.sql'

XML配置

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="url" value="${my.datasource.url}"/>
    <!-- other properties for username, password etc... -->
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="myDataSource"/>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="myDataSource"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

许多采用以下模式的Java类

@Component
public class SomethingDAOImpl implements SomethingDAO {
  @Autowired
  public SomethingDAOImpl(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
  }
}

@Component
public class SomethingElseDAOImpl implements SomethingElseDAO {
  @Autowired
  public SomethingElseDAOImpl(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
  }
}

在默认的bean作用域为singleton的情况下,我认为这会起作用,但是我想我缺少了一些东西.另外,如果我切换到已经具有表和参考数据设置的真实Oracle实例,则测试全部通过.

With the default bean scope being singleton I thought this would just work, but I guess i'm missing something. Also, if I switch to an real Oracle instance that already has the tables and reference data set-up, the tests all pass.

推荐答案

我最终使用了另一种方法,因为我无法以可以无错误地重新应用的方式编写SQL.

I ended up using an alternative approach, as I could not write the SQL in a way that could be reapplied without error.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">

    <jdbc:initialize-database data-source="myDataSource" enabled="true" ignore-failures="ALL">
        <jdbc:script location="classpath:create-and-alter-tables-first-then-add-test-data.sql" />
    </jdbc:initialize-database>
</beans>

在上下文初始化时执行一次.

which is executed once at context initialization.

注意:为简便起见,省略了其他名称空间和bean.

这篇关于@Autowired jdbcTemplate和h2内存数据库多次执行runscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆