zoukankan      html  css  js  c++  java
  • SSM框架中添加事务

    1.今天在公司中做了一组同时有两个动作的操作,由于以前的项目在家里电脑里,配置记不住了,

    在网上找了spring事务的配置

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

    <!-- 注解方式配置事物 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 拦截器方式配置事物 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="add*" propagation="REQUIRED" />
    <tx:method name="append*" propagation="REQUIRED" />
    <tx:method name="insert*" propagation="REQUIRED" />
    <tx:method name="save*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="modify*" propagation="REQUIRED" />
    <tx:method name="edit*" propagation="REQUIRED" />
    <tx:method name="delete*" propagation="REQUIRED" />
    <tx:method name="remove*" propagation="REQUIRED" />
    <tx:method name="repair" propagation="REQUIRED" />
    <tx:method name="delAndRepair" propagation="REQUIRED" />

    <tx:method name="get*" propagation="SUPPORTS" />
    <tx:method name="find*" propagation="SUPPORTS" />
    <tx:method name="load*" propagation="SUPPORTS" />
    <tx:method name="search*" propagation="SUPPORTS" />
    <tx:method name="datagrid*" propagation="SUPPORTS" />

    <tx:method name="*" propagation="SUPPORTS" />
    </tx:attributes>
    </tx:advice>
    <aop:config>

    <!-- 这个配置很重要,切面的切入位置-->
    <aop:pointcut id="transactionPointcut"
    expression="execution(* com.heb.user.service..*Impl.*(..))" />
    <aop:advisor pointcut-ref="transactionPointcut"
    advice-ref="transactionAdvice" />

    </aop:config>

    service层也是运用的@Transactional注解,直接加在方法上,只对此方法生效

    需要事物的具体操作需要try{}catch(){}

    具体例子

    try{
    i = numeralLoginDao.insertInToResellAll(resellCar);//操作1
    j = numeralLoginDao.deleteTemporaryResell(resellCar.getPhonenumber());//操作2
    if(i==0||j==0){
    throw new Exception();
    }

    }catch(Exception e){
    System.out.println("数据操作错误");
    }

    这样只要发生定义的和未知的错误都会回滚!

  • 相关阅读:
    easyUI中textbox或number的数值大小校验
    个人附加作业
    个人最终总结
    结对编程————电梯调度
    visual studio 2013的使用和单元测试
    第三次作业(二)
    团队作业之个人总结篇
    结队编程之总结篇
    电梯调度系统(界面由C图形库编绘)
    软件工程作业单词统计
  • 原文地址:https://www.cnblogs.com/1736gerr/p/6933220.html
Copyright © 2011-2022 走看看