【问题标题】:runscript executed multiple times for @Autowired jdbcTemplate and h2 in-memory database为 @Autowired jdbcTemplate 和 h2 内存数据库多次执行 runscript
【发布时间】:2023-03-24 08:15:01
【问题描述】:

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

我可以看到问题在于RUNSCRIPT 中引用的脚本被多次执行,因此产生了Index "XXX_IDX" already exists 错误和其他违规行为。那么有没有办法强制脚本只运行一次,还是我需要一个外部数据库?似乎该脚本在我假设为 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 范围是单例的,我认为这可以正常工作,但我想我错过了一些东西。此外,如果我切换到已经设置了表和参考数据的真实 Oracle 实例,测试全部通过。

【问题讨论】:

    标签: spring-mvc h2


    【解决方案1】:

    我最终使用了另一种方法,因为我无法以一种可以在没有错误的情况下重新应用的方式编写 SQL。

    <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>
    

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

    注意:为简洁起见,省略了其他命名空间和 bean。

    【讨论】:

      【解决方案2】:

      在很多情况下,可以编写 SQL 脚本,这样就不会抛出异常:

      create table if not exists test(id int, name varchar(255));
      create index if not exists test_idx on test(name);
      

      【讨论】:

      • 谢谢。我已经进行了一些类似的更改,但似乎我无法在 Oracle 中轻松地为序列做同样的事情。我确实首先搜索了“创建或替换序列” :-) 如果可能的话,我想避免重写所有现有的 SQL,因此“初始化数据库一次”的解决方案会更可取。
      猜你喜欢
      • 1970-01-01
      • 2020-12-07
      • 2017-06-16
      • 2022-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 2016-06-08
      相关资源
      最近更新 更多