- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有问题要问 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/
如果我执行以下操作 session.getTransaction().rollback(); 然后可以/可以在同一个 session 对象上开始调用保存/更新/删除等吗?无法在 Hibernate S
在Sqlite3中,有没有办法在提交事务后回滚到某个点? 如果您想知道如果我不确定我为什么要提交一个语句,原因是我在 C++ 接口(interface)内使用 Sqlite,只有在我提交事务时才会保存
经过一段时间的阅读和测试不同形式的加载EntityManager实例和bean以进行一些嵌套事务,最后我没有找到一种方法,在运行时异常之后,数据进行完全回滚,必须接近阶段到什么我想要的是只有一个实体保
我们有一个应用程序,它读取 JMS 并执行一些数据库事务,所有这些都是 XA 事务的一部分。如果我们发现消息有问题,我们想要回滚数据库,但我们不希望 JMS 回滚让我们再次读取消息(如果先前已启动回滚
我有一个情况,我需要回滚数据库上的更改,而且我想保留插入的日志。 插图: Add processing log Insert some data Insert some data Insert
我定义了这样一个函数- ... DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK; DECLARE EXIT HANDLER FOR SQLWARNING
我的应用程序中有一个模型和一个类似商店的东西。模型对象使用 promise 等待存储中的数据。 但在某些情况下我需要删除模型,但因为我使用了 promise 我的模型仍处于关闭状态并且 GC 无法清理
我错误地触发了更新查询,并且 dat 字段中表中的所有记录都已更新,并且由于 auto_commit 处于打开状态,因此也无法回滚。 还有其他方法可以检索记录吗? 最佳答案 我不认为一旦提交查询/事务
我了解我可以使用 MySql 的命令 BACKUP 和 RESTORE 来备份数据库并在需要时回滚。 我的问题是,我是否能够以这种方式执行它: sql="BACKUP my_db TO DISK my
如果 MySQL 中仍有未提交/未回滚的事务,如何让我的事务等待? 目前,我在代码端进行事务处理,而不是在数据库存储过程端,如下所示: cmd.Connection.BeginTransaction(
这是以下内容的后续内容: php/mysql transaction not rolling back on failure (Codeigniter framework) 我在上面的链接中实现了建议
我只是构建了一个存储过程来测试 mysql 中的事务。 但不知何故它不起作用。 这是我的代码: USE `test`; DROP procedure IF EXISTS `testTran`; DEL
我有一个 Service Fabric 应用程序。比方说,版本 1.0.0。我将更新部署为版本 2.0.0。一切都很好。 稍后,我想回滚(或根据您的观点升级)到版本 1.0.0。 我想运行一个简单的P
我不知道这是否正确,但由于某种原因,我的存储过程在发生异常后没有回滚。所以即使我遇到异常,我的插入语句也会被提交 我忘记了什么吗? PROCEDURE SP_USUARIO_INSERT
这个问题与我的问题有关:SQL Server and TransactionScope (with MSDTC): Sporadically can't get connection 我正在使用 .n
登录到我正在使用的Oracle数据库的用户没有提交或回滚就注销了,现在我的数据库无法更改,它似乎已被锁定。 我试过了 rollback 但这没用。 有什么想法吗? 谢谢。 最佳答案 不提交或不回滚就
这只是一个 RStudio 界面问题: 当回滚控制台查看我的工作和输出时,我只能回滚到目前为止。有没有办法打开控制台并回滚我所做的一切? 有时我想检查一个很长的列表的结果,或者我忘记在脚本框而不是控制
是否可以在 Heroku 上撤消回滚? 我必须在实时环境中尝试一些东西,进行测试,然后恢复到原始状态(在回滚之前)。 Heroku 是否能够顺利做到这一点,否则会导致问题? 最佳答案 是的,您可以滚动
我想要 仅回滚: 回滚:2015_05_15_195423_alter_table_web_directories 我运行 php artisan migrate:rollback,我的 3 个迁移正
我正在使用 MQQueueSession.rollback() 来回滚工作单元。消息被移至 BACKOUT 队列,这样就可以了。 问题是如何处理回退队列中的这些消息?我读到我可以使用 runmqdlq
我是一名优秀的程序员,十分优秀!