Spring错误——JUnit事务自动回滚
背景
- 在Spring 单元测试中,使用的是JUnit4进行测试。测试过程不报错,但是数据不能插入到数据库中。
- 测试代码如下
@Test @Transactional public void saveElec() { elecService.saveElec(elecEntity); ElecEntity temp_elec = elecService.getElecById(elec.getId()); Assert.assertNotNull("插入数据库失败", temp_elec); }
日志
三月 09, 2021 13:11:22 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
三月 09, 2021 13:11:22 下午 org.springframework.orm.hibernate5.HibernateTransactionManager afterPropertiesSet
信息: Using DataSource [org.apache.commons.dbcp2.BasicDataSource@5656be13] of Hibernate SessionFactory for HibernateTransactionManager
三月 09, 2021 4:31:33 下午 org.springframework.test.context.transaction.TransactionContext startTransaction
信息: Began transaction (1) for test context [DefaultTestContext@41fed14f testClass = UserServiceImplTest, testInstance = com.ths.demo3.service.serviceImpl.UserServiceImplTest@4d6ee47, testMethod = saveUser@UserServiceImplTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@6818d900 testClass = UserServiceImplTest, locations = '{classpath:applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[org.springframework.test.context.web.socket.MockServerContainerContextCustomizer@6442b0a6], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.orm.hibernate5.HibernateTransactionManager@2c306a57]; rollback [true]
三月 09, 2021 4:31:33 下午 org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
三月 09, 2021 4:31:33 下午 org.hibernate.hql.internal.ast.HqlSqlWalker generatePositionalParameter
WARN: [DEPRECATION] Encountered positional parameter near line 1, column 41 in HQL: [from com.ths.demo3.vo.User u where u.id=?]. Positional parameter are considered deprecated; use named parameters or JPA-style positional parameters instead.
三月 09, 2021 4:31:33 下午 org.springframework.test.context.transaction.TransactionContext endTransaction
信息: Rolled back transaction for test: [DefaultTestContext@41fed14f testClass = UserServiceImplTest, testInstance = com.ths.demo3.service.serviceImpl.UserServiceImplTest@4d6ee47, testMethod = saveUser@UserServiceImplTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@6818d900 testClass = UserServiceImplTest, locations = '{classpath:applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[org.springframework.test.context.web.socket.MockServerContainerContextCustomizer@6442b0a6], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]]
原因
- JUnit环境
@Transactional
注解下,存在注解@Rollback,表示对事物进行操作,默认为true
解决方法
- 测试方法加上注解 @Rollback(true)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
2018-03-09 CentOS7下Docker中构建Jenkins容器