gpt4 book ai didi

spring - 事务不适用于 Spring 3.1 – H2 – junit 4– hibernate 3.2

转载 作者:行者123 更新时间:2023-12-01 10:57:03 24 4
gpt4 key购买 nike

我有以下测试..

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/schedule-agents-config-context.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class H2TransactionNotWorkingTest extends SubmitAgentIntegratedTestBase {
private static final int FIVE_SUBMISSIONS = 5;

@Autowired
private ApplicationSubmissionInfoDao submissionDao;

private FakeApplicationSubmissionInfoRepository fakeRepo;

@Before
public void setUp() {
fakeRepo = fakeRepoThatNeverFails(submissionDao, null);
submitApplication(FIVE_SUBMISSIONS, fakeRepo);
}

@Test
@Rollback(true)
public void shouldSaveSubmissionInfoWhenFailureInDatabase() {
assertThat(fakeRepo.retrieveAll(), hasSize(FIVE_SUBMISSIONS));

}

@Test
@Rollback(true)
public void shouldSaveSubmissionInfoWhenFailureInXmlService() {
assertThat(fakeRepo.retrieveAll().size(), equalTo(FIVE_SUBMISSIONS));
}
}

...以及以下配置...

   <jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:/db/h2-schema.sql" />
</jdbc:embedded-database>

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

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

<bean id="transactionalSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
</props>
</property>
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy"/>
</property>
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
<property name="packagesToScan" value="au.com.mycomp.life.snapp"/>
</bean>

<bean id="regionDependentProperties" class="org.springframework.core.io.ClassPathResource">
<constructor-arg value="region-dependent-service-test.properties"/>
</bean

>

我还在 sql 脚本中将自动提交设置为 false

SET AUTOCOMMIT FALSE;

代码中没有REQUIRES_NEW。为什么回滚在测试中不起作用?

干杯普拉宾

最佳答案

我遇到了同样的问题,但我终于解决了它,尽管我不使用 Hibernate(应该无关紧要)。

让它发挥作用的关键是扩展正确的 Spring 单元测试类,即 AbstractTransactionalJUnit4SpringContextTests。注意类名中的“Transactional”。因此,工作事务单元测试类的框架如下所示:

@ContextConfiguration(locations = {"classpath:/com/.../testContext.xml"})
public class Test extends AbstractTransactionalJUnit4SpringContextTests {

@Test
@Transactional
public void test() {
}
}

关联的 XML 上下文文件包含以下项目:

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

<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

使用此设置,每个测试方法的修改都会正确回滚。

问候,奥拉

关于spring - 事务不适用于 Spring 3.1 – H2 – junit 4– hibernate 3.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14574988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com