Populating H2 database with Spring and jdbc DataSourceTransactionManager

和自甴很熟 提交于 2019-12-13 06:46:32

问题


I am trying to use H2 embedded database with script config to create and load database during test.

It works fine but when I tried to add transaction support for my database interactions I am getting error during context initialization.

Here is my spring config:

<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:mem:embeddedH2Database;MODE=Oracle;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

<jdbc:embedded-database id="embeddedH2Database" type="H2">
    <jdbc:script location="classpath:db/sql/c`enter code here`reate-db.sql" />
    <jdbc:script location="classpath:db/sql/insert-data.sql" />
</jdbc:embedded-database>


<tx:advice id="txAdvice" transaction-manager="txManager">
    <!-- the transactional semantics... -->
    <tx:attributes>
        <tx:method name="*" timeout="60" />
    </tx:attributes>
</tx:advice>

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

<aop:config>
    <aop:pointcut id="serviceOperation" expression="execution(* x.y.service.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
</aop:config>

Here is bean creation error:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedH2Database': Cannot create inner bean '(inner bean)#23bff419' of type [org.springframework.jdbc.datasource.init.CompositeDatabasePopulator] while setting bean property 'databasePopulator'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#23bff419': Cannot create inner bean '(inner bean)#1d2bd371' of type [org.springframework.jdbc.datasource.init.ResourceDatabasePopulator] while setting bean property 'populators' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1d2bd371': Cannot create inner bean '(inner bean)#65fe9e33' of type [org.springframework.jdbc.config.SortedResourcesFactoryBean] while setting bean property 'scripts'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#65fe9e33': Post-processing of FactoryBean's object failed; nested exception is java.lang.AssertionError


回答1:


Since I was using transactions it was clashing with execution of scripts to create tables and load data.

These scripts need to be executed outside without applying transaction semantics added by spring config.

So I changed embedded database bean as follows to remove script elements:

<jdbc:embedded-database id="embeddedH2Database" type="H2"/>

And added @Sql annotation before my test to create table and load table.

@Sql({"classpath:db/sql/create-db.sql", "classpath:db/sql/insert-data.sql"})


来源:https://stackoverflow.com/questions/42403336/populating-h2-database-with-spring-and-jdbc-datasourcetransactionmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!