zoukankan      html  css  js  c++  java
  • Spring mvc注解方式使用事务回滚

    项目名:1ma1ma

    jdbc.xml

        <bean  id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 
               p:driverClassName="${jdbc.driverClassName}"
               p:url="${jdbc.url}"
               p:username="${jdbc.username}"
               p:password="${jdbc.password}"
               p:maxActive="${jdbc.pool-size}"
               p:minEvictableIdleTimeMillis="600000" p:timeBetweenEvictionRunsMillis="900000"
               p:removeAbandoned="true" p:logAbandoned="true" p:removeAbandonedTimeout="120"
               p:maxWait="5000"
        />

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
            <property name="dataSource">  
                <ref local="dataSource" />  
            </property>  
        </bean>  
        <tx:annotation-driven transaction-manager="transactionManager" />
        <context:component-scan base-package="com.yaoma.*">   
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />   
        </context:component-scan>  



    servlet-context.xml


        <context:component-scan base-package="com.yaoma.*" >   
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />   
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />    
        </context:component-scan>  



        @Transactional
        public int delete(String IMGroupID, String userid) throws Exception {
            //依照IMGroupID和userid更新相应的Groups表内容
            int is_return = Groups_Dao.IsDeleted_Groups(IMGroupID, userid, 0);
            if (is_return == 0) {
                throw new RuntimeException("is_return is 0;");
            }


            //去环信上删除相应的数据
            String hx_return = HX_Service.delete_group(IMGroupID);
            if (hx_return == null) {
                throw new RuntimeException("hx_return is null");
            }
            return Integer.parseInt(hx_return);
        }



        @Transactional注解标签

    由于在执行中,所以使用RuntimeException进行异常抛出,类中不能使用try捕捉异常,直接throws Exception抛出异常

    如执行时使用Exception抛出异常,则回滚无效

  • 相关阅读:
    2019-1-7 水晶报表
    2018-12-25工作记录 空白行===水晶报表
    2018-7-26-随笔-泛型
    2018-7-20-随笔-转换
    2018-7-18-随笔-接口
    2018-7-17-随笔-params和ref、out用法、事件访问器
    VPS安装metasploit-framework
    Mimiktaz抓取本机密码
    msfvenom生成各类Payload命令
    docker容器开启ssh远程登录
  • 原文地址:https://www.cnblogs.com/llguanli/p/6767259.html
Copyright © 2011-2022 走看看