Spring事务
一个经常忽略的问题
@Service
public class DemoService {
@Autowired
private PayMapper payMapper;
@Transactional
public void aaa() throws RuntimeException {
Pay pay = new Pay();
pay.setUserid(0L);
pay.setChannel("");
pay.setMoney(0);
pay.setPaylevel(0);
pay.setOrderid("ABCDEFG");
pay.setTime(0L);
payMapper.insert(pay);
payMapper.insert(pay);
}
}
此处,经常出现事务配置好了但是事务不生效的情况,看 @Transactional 注解中的说明可知,
默认回滚只针对,方法抛出RuntimeException 和 Error 生效
若不抛出或者用 Exception抛出都不会触发事务回滚
(为了说明换了一个,xml的配置展示清晰一点)
一般公司的架构师针对Dao层基本都做了的 rollback-for 的配置
所以有时候我们感觉Exception也可以抛出啊,其实都有大牛帮我们考虑到了。
感谢大牛!!
注解回滚配置: @Transactional(rollbackFor = {Exception.class,RuntimeException.class, SQLException.class})
声明式事务的两种配置
xml
<?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:sharding="http://shardingjdbc.io/schema/shardingjdbc/sharding"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://shardingjdbc.io/schema/shardingjdbc/sharding http://shardingjdbc.io/schema/shardingjdbc/sharding/sharding.xsd">
<!--DruidDataSource (略)-->
<!--事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource "/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:cn/joeg/demo/db/mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.joeg.demo.db.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
annotation
@Configuration
@MapperScan(basePackages = "cn.joeg.demo.db.mapper")
@EnableTransactionManagement(proxyTargetClass = true)
public class MybatisConfig {
@Autowired
@Qualifier("dataSource")
public DataSource dataSource;
@Lazy(false)
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory localSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
// sqlSessionFactoryBean.setTypeHandlers(new TypeHandler[]{new TestTypeHandle(),new GPTypeHandler()});
// sqlSessionFactoryBean.setPlugins(new Interceptor[]{new TestPlugin()});
// sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageInterceptor()});
SqlSessionFactory factory = sqlSessionFactoryBean.getObject();
factory.getConfiguration().setLazyLoadingEnabled(true);
factory.getConfiguration().setAggressiveLazyLoading(false);
factory.getConfiguration().setProxyFactory(new CglibProxyFactory());
return factory;
}
private PageInterceptor pageInterceptor() {
PageInterceptor pageInterceptor = new PageInterceptor();
Properties properties = new Properties();
properties.put("helperDialect", "mysql");
pageInterceptor.setProperties(properties);
return pageInterceptor;
}
@Primary
@Lazy(false)
@Bean(name = "sqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(localSessionFactoryBean(), ExecutorType.SIMPLE);
}
@Lazy(false)
@Bean(name = "batchSst")
public SqlSessionTemplate batchSst() throws Exception {
return new SqlSessionTemplate(localSessionFactoryBean(), ExecutorType.BATCH);
}
@Bean(name = "txManager")
public DataSourceTransactionManager dataSourceTransactionManager() {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
}
}
如何使用
没什么好讲的