ssm事务、spring+ibatis+junit调用存储过程事务不提交解决方法、测试类事务不提交解决方法

94 篇文章 0 订阅
60 篇文章 2 订阅

参考:http://blog.csdn.net/molingduzun123/article/details/49383235

问题:spring+ibatis+junit调用存储过程,事务不提交(rolled back)、ssm事务不提交解决方法。


遇到打印日志如下,在用junit进行单元测试时,调用sql server库的存储过程,会报如下问题,即事务自动回滚:

 (1) 明明已经插入了数据,但是却没有提交到数据库中:

(2)spring+ibatis+junit遇到无法提交到数据库问题:

[java] view plain copy
  1. 10:44:56.682 [main] WARN  - Unable to proxy method [public final void org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests.setApplicationContext(org.springframework.context.ApplicationContext)] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.  
  2. 10:44:56.777 [main] INFO  - Began transaction (1for test context [DefaultTestContext@54b07eeb testClass = DeviceControllerTest, testInstance = com.tonson.app.push.api.user.DeviceControllerTest@f5ca1bd, testMethod = testClientSetting@DeviceControllerTest, testException = [], mergedContextConfiguration = [MergedContextConfiguration@a7e872c testClass = DeviceControllerTest, locations = '{classpath:spring/applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = []]]; transaction manager [org.springframework.jdbc.datasource.DataSourceTransactionManager@78dc5f15]; rollback [true]  
  3. 10:44:56.793 [main] DEBUG - {conn-100000} Connection  
  4. 10:44:56.820 [main] DEBUG - {conn-100000} Preparing Call: {call UP_UserConfig_Save1 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}  
  5. 10:44:56.846 [main] DEBUG - {pstm-100001} Executing Statement: {call UP_UserConfig_Save1 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}  
  6. 10:44:56.846 [main] DEBUG - {pstm-100001} Parameters: [011111121110111118151445111]  
  7. 10:44:56.847 [main] DEBUG - {pstm-100001} Types: [java.lang.Integer, java.lang.String, java.lang.String, java.lang.Integer, java.lang.String, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.String]  
  8. {"lan":false,"message":"成功","returncode":0}  
  9. 10:44:56.984 [main] INFO  - Rolled back transaction after test execution for test context [DefaultTestContext@54b07eeb testClass = DeviceControllerTest, testInstance = com.autohome.app.push.api.user.DeviceControllerTest@f5ca1bd, testMethod = testClientSetting@DeviceControllerTest, testException = [], mergedContextConfiguration = [MergedContextConfiguration@a7e872c testClass = DeviceControllerTest, locations = '{classpath:spring/applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = []]]  
  10. 10:44:56.988 [Thread-1] INFO  - Closing org.springframework.context.support.GenericApplicationContext@79ed3030: startup date [Sat Oct 24 10:44:54 CST 2015]; root of context hierarchy  

如果此时测试的本意是要更新库里数据,则需要在对应测试的方法上加注解:@Rollback(false),如:

加上了这句以后则可以正常地在测试类提交信息到数据库中了。

[java] view plain copy
  1. @Test  
  2. @Rollback(false)//防止事务自动回滚  
  3. public void testClientSetting(){  
  4. }  
当然,在使用事务相关注解时,需要声明式事务,即在applicationContext.xml文件中添加:

[html] view plain copy
  1. <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false" />  
  2.        
  3. <!-- 定义事务 -->  
  4. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  5.     <property name="dataSource" ref="pushApiDataSource" />  
  6. </bean>  

正常运行如图:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值