gpt4 book ai didi

java - Spring @Transactional 方法不回滚

转载 作者:行者123 更新时间:2023-11-28 20:55:00 27 4
gpt4 key购买 nike

我有问题要问 Spring、hibernate 和 testng。

我正在开发一个应用程序并尝试编写一个事务性单元测试。问题是,当我的业务方法被标记为“事务性”时,我如何回滚我的数据库操作?

代码:

@Test
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@TransactionConfiguration(defaultRollback = true)
public class SampleTest extends
AbstractTransactionalTestNGSpringContextTests {


@Autowired
private AuthorDao authorDao;

@BeforeTest
void createAppCtx() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"/applicationContext.xml");

}


@Test
void testStg() {

Person person = new Author();
person.setFirstName("Edward");
person.setLastName("Kowalski");
authorDao.createAuthor(person);
}

在我的 authorDao 中,我有以下方法:

@Repository
@Transactional
public class AuthorDao {

@PersistenceContext
private EntityManager entityManager;


public AuthorDao() {

}

public AuthorDao(EntityManager entityManager) {
this.entityManager = entityManager;
}

public Author createAuthor(Person author) {
entityManager.persist(author);
return (Author) author;
}

如果需要应用上下文,我也可以附加它。
因此,正如您所见,业务方法是事务性的,因此调用后会进行提交。那么重点是如何避免在测试类中提交?是否可以?

非常感谢您的帮助。

编辑:应用上下文:

  <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


<context:component-scan base-package="pl.hs" />
<mvc:annotation-driven />
<tx:annotation-driven transaction-manager="myTxManager" />


<beans>


<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>


<bean id="jdbcPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="classpath:project.properties" />

<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${hibernate.connection.driver_class}"
p:url="${hibernate.connection.url}"
p:username="${hibernate.connection.username}"
p:password="${hibernate.connection.password}" />


<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="myDataSource" />
</bean>

<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="persistenceUnitName" value="pl.hs" />
</bean>

<bean id="myTxManager" name="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
<!-- <property name="dataSource" ref="myDataSource" /> -->
</bean>

</beans>
</beans>

持久化.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

<persistence-unit name="pl.hs"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

<class> myJavaClasses </class>




<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>

<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"></property>
<property name="hibernate.show_sql" value="true" />



<property name="hibernate.hbm2ddl.auto" value="create"></property>
</properties>


</persistence-unit>

最佳答案

也将@Transactional 添加到测试方法,以便应用 TransactionConfiguration。

关于java - Spring @Transactional 方法不回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28045795/

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