• SSM 事务


    配置文件

    <!-- 定义事务管理器 -->
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- 定义事务策略 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <!--所有以query开头的方法都是只读的 -->
    <tx:method name="query*" read-only="true" />
    <tx:method name="get*" read-only="true" />
    <tx:method name="find*" read-only="true" />
    <tx:method name="select*" read-only="true" />
    <!--其他方法使用默认事务策略 -->
    <tx:method name="*" rollback-for="Throwable"/>
    </tx:attributes>
    </tx:advice>

    <aop:config>
    <aop:pointcut id="myPointcut" expression="execution(* cc.openkit..service.*.*(..))" />
    <!--将定义好的事务处理策略应用到上述的切入点 -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
    </aop:config>


    service层


    @Service
    public class AppConfigService extends BaseService<AppConfig>{

    @Autowired
    private AppConfigMapper appConfigMapper;

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void test() {

    AppConfig appConfig = new AppConfig();
    appConfig.setUpBaseId(AppUtil.uuid());
    AppConfig appConfig1 = new AppConfig();
    appConfig1.setUpBaseId(AppUtil.uuid());
    Integer integer = this.saveSelect(appConfig);
    Integer integer1 =this.saveSelect(appConfig1);
    Integer num=integer + integer1;
    if (num==2){
    System.out.println(1);
    throw new RuntimeException("出现异常了");
    }else{
    }
    }
    }

    抛出异常事物回滚
  • 相关阅读:
    gif&png&jpg&webp
    设计点滴&css效果点滴
    backbone点滴
    js自己总结的小东西(打印出来方便学习)
    nodejs点滴
    js类型
    mongo学亮的分享
    npm package.json中的dependencies和devDependencies的区别
    161130、Dubbo+SpringMVC工程创建详解
    161129、详解5种跨域方式及其原理
  • 原文地址:https://www.cnblogs.com/SeaWxx/p/8124830.html
Copyright © 2020-2023  润新知