Spring XML tx:advice相当于@TransactionConfiguration?

发布于 2025-01-02 02:48:11 字数 1490 浏览 1 评论 0原文

我在 Spring 3.0.5 中使用基于 XML 的事务配置。转向基于注释并不是一种选择。基于 XML 的配置中 @TransactionConfiguration( rollback=true) 的等价物是什么?

目前,我没有在 JUnit 中强制回滚,因此我的数据库始终充满必须删除/清理的随机测试数据。此外,我最终在不同的测试运行之间遇到了 PK 冲突。

我的 applicatonContext 内容如下:

...
<!-- Wrap all DAO Implementations in a transaction -->
<aop:config proxy-target-class="false">
    <aop:pointcut id="daoOperation" expression="execution(* com.calculator.dao.impl.*Impl.* (..))" />
    <aop:advisor pointcut-ref="daoOperation" advice-ref="txAdvice" />
</aop:config>

<tx:advice id="txAdvice">
    <tx:attributes>
        <tx:method name="*" read-only="true" propagation="REQUIRED"/>
        <tx:method name="execute*" propagation="REQUIRED"/>
        <tx:method name="query*" propagation="REQUIRED"/>
        <tx:method name="insert" propagation="REQUIRED"/>
        <tx:method name="delete" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

<bean class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="${datasource.url}" />
    <property name="username" value="${datasource.user}" />
    <property name="password" value="${datasource.password}" />
    <property name="defaultAutoCommit" value="false" />
</bean>

...

如何指定我希望所有 JUnit 默认回滚?

谢谢,

埃里克

I'm using XML based transaction configuration in Spring 3.0.5. Moving to annotation based is not an option. What is the equivalent of @TransactionConfiguration( rollback=true) in XML based configuration?

At the moment, I am not enforcing rollbacks in my JUnits, so my DB is getting consistently full of random test data that must be dropped/cleaned. Additionally, I end up having PK conflicts between different test runs.

My applicatonContext reads as follows:

...
<!-- Wrap all DAO Implementations in a transaction -->
<aop:config proxy-target-class="false">
    <aop:pointcut id="daoOperation" expression="execution(* com.calculator.dao.impl.*Impl.* (..))" />
    <aop:advisor pointcut-ref="daoOperation" advice-ref="txAdvice" />
</aop:config>

<tx:advice id="txAdvice">
    <tx:attributes>
        <tx:method name="*" read-only="true" propagation="REQUIRED"/>
        <tx:method name="execute*" propagation="REQUIRED"/>
        <tx:method name="query*" propagation="REQUIRED"/>
        <tx:method name="insert" propagation="REQUIRED"/>
        <tx:method name="delete" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

<bean class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="${datasource.url}" />
    <property name="username" value="${datasource.user}" />
    <property name="password" value="${datasource.password}" />
    <property name="defaultAutoCommit" value="false" />
</bean>

...

How do I specify that I want all my JUnits to rollback by default?

Thanks,

Eric

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

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

发布评论

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

评论(1

守护在此方 2025-01-09 02:48:11

将这个配置放在您的 JUnit 测试类上。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/pathTo/spring/context/applicationContext.xml")
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class LocationDaoTest extends AbstractTransactionalJUnit4SpringContextTests{

  @Test
  public void testDAOsCRUD(){}
}

我希望我已经为您提供了有关您问题的所有答案。

Put on your JUnit test class this configuration.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/pathTo/spring/context/applicationContext.xml")
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class LocationDaoTest extends AbstractTransactionalJUnit4SpringContextTests{

  @Test
  public void testDAOsCRUD(){}
}

I hope I've given you all the answers about your question.

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