mybatis-spring 批处理自动回滚,不报错
【问题】按照mybatis的官网上配置,但是结果能够成功运行,但是数据库中没有提交成功
开启log4j查看日志后发现,在执行完所有的处理后,自动回滚。查来查去都是同样一个问题
【解决】不知道为什么,datasourcetransaction会创建两次事务(这个原因可能得留给看官们自己去研究下,如果有结果了,希望大家能反馈下给我哈),于是只能设置propagation为NOT_SUPPORTED,来挂起事务。具体代码如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:application.xml"})
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class SpringTest {
@Autowired
EmployeeMapper employeeMapper;
@Autowired
SqlSessionFactory sqlSessionFactory;
@Autowired
SqlSession sqlSession;
@Autowired
DataSourceTransactionManager dataSourceTransactionManager;
@Test
public void test1() {
System.out.println(employeeMapper.selectAll());
}
@Test
public void test2() {
long begin = System.currentTimeMillis();
// SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
TransactionStatus txStatus = dataSourceTransactionManager.getTransaction(new DefaultTransactionDefinition());
System.out.println(txStatus.isNewTransaction());
try {
for (int i = 0; i < 10000; i++) {
Employee employee = new Employee();
employee.setLastName(String.valueOf(i));
employee.setEmail(String.valueOf(i) + "@atguigu.com");
employee.setGender(i % 2 == 0 ? 1 : 0);
mapper.insert(employee);
}
} catch (Exception e) {
// transactionManager.rollback(txStatus);
// sqlSession.rollback();
e.printStackTrace();
}
dataSourceTransactionManager.commit(txStatus);
// sqlSession.commit();
long end = System.currentTimeMillis();
System.out.println("完成,总耗时长" + (end - begin) / 1000 + "秒");
}
}
xml配置文件:
<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.atguigu.sssmintegrationproject">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:property-placeholder location="classpath:sqlinfo.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc-user}"/>
<property name="password" value="${jdbc-pwd}"/>
<property name="driverClassName" value="${jdbc-driver}"/>
<property name="url" value="${jdbc-url}"/>
</bean>
<!-- 配置和MyBatis的整合-->
<bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlsessionFactory"/>
<constructor-arg index="1" value="BATCH" />
</bean>
<!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描所有dao接口的接口,加入到ioc容器中-->
<property name="basePackage" value="com.atguigu.sssmintegrationproject.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlsessionFactory"/>
</bean>
<!-- 配置事务-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>
<aop:config>
<!-- 切入点表达式-->
<aop:pointcut id="aoppc"
expression="execution(* com.atguigu.sssmintegrationproject.service..*(..))"/>
<!-- 配置事务增强-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="aoppc"/>
</aop:config>
<!-- 配置事务增强,-->
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<!-- *的作用代表所有的方法都是事务方法-->
<tx:method name="*"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- Spring配置文件的核心点(数据源,mybatis整合,事务)-->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.ArithmeticException">error</prop>
</props>
</property>
</bean>
</beans>