参考:http://blog.csdn.net/molingduzun123/article/details/49383235
问题:spring+ibatis+junit调用存储过程,事务不提交(rolled back)、ssm事务不提交解决方法。
遇到打印日志如下,在用junit进行单元测试时,调用sql server库的存储过程,会报如下问题,即事务自动回滚:
(1) 明明已经插入了数据,但是却没有提交到数据库中:
(2)spring+ibatis+junit遇到无法提交到数据库问题:
- 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.
- 10:44:56.777 [main] INFO - Began transaction (1) for 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]
- 10:44:56.793 [main] DEBUG - {conn-100000} Connection
- 10:44:56.820 [main] DEBUG - {conn-100000} Preparing Call: {call UP_UserConfig_Save1 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}
- 10:44:56.846 [main] DEBUG - {pstm-100001} Executing Statement: {call UP_UserConfig_Save1 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}
- 10:44:56.846 [main] DEBUG - {pstm-100001} Parameters: [0, 111, 111, 2, 111, 0, 111, 1, 1, 815, 1445, 111]
- 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]
- {"lan":false,"message":"成功","returncode":0}
- 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: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),如:
加上了这句以后则可以正常地在测试类提交信息到数据库中了。
- @Test
- @Rollback(false)//防止事务自动回滚
- public void testClientSetting(){
- }
- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false" />
- <!-- 定义事务 -->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="pushApiDataSource" />
- </bean>
正常运行如图: