Spring事务应用

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抛出都不会触发事务回滚

Spring事务应用_第1张图片

(为了说明换了一个,xml的配置展示清晰一点)

一般公司的架构师针对Dao层基本都做了的 rollback-for 的配置

所以有时候我们感觉Exception也可以抛出啊,其实都有大牛帮我们考虑到了。

感谢大牛!!

Spring事务应用_第2张图片

 

注解回滚配置: @Transactional(rollbackFor = {Exception.class,RuntimeException.class, SQLException.class})

声明式事务的两种配置

xml




    
 
    
    
    
        
    

    
        
        
    

    
        
        
    

 

 

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;
    }

}

 

如何使用

没什么好讲的

你可能感兴趣的:(Spring事务应用)