spring aop 切面编程控制管理事务

1   一个业务逻辑方法中需要有操作插入两张表记录相互关联依赖数据,

2   要保证两张表要么都插入成功要么都插入失败,

3   否则只成功插入任何一张表都是毫无意义的垃圾数据


本人想到spring AOP 事务管理能支持此业务需求功能,

具体配置如下经验证:

1 DAO接口定义显示抛出异常

2 产生异常处catch时候 { throw new  runtimeException} 这个是spring默认回滚的异常


<!--aop-->配置

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


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

    <bean name="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="create*" propagation="REQUIRED"
                rollback-for="java.lang.Exception" />
            <tx:method name="remove*" propagation="REQUIRED"
                rollback-for="java.lang.Exception" />
            <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
            <tx:method name="save*" propagation="REQUIRED"
                rollback-for="java.lang.Exception" />
            <tx:method name="delete*" propagation="REQUIRED"
                rollback-for="java.lang.Exception" />
            <tx:method name="update*" propagation="REQUIRED"
                rollback-for="java.lang.Exception" />
            <tx:method name="insert*" propagation="REQUIRED"
                rollback-for="java.lang.Exception" />
            <tx:method name="log*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
            <tx:method name="next*" propagation="REQUIRED"
                rollback-for="java.lang.Exception" />
            <tx:method name="*" read-only="true" />
            <tx:method name="do*" propagation="REQUIRED"
                rollback-for="java.lang.Exception" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:advisor pointcut="execution(* com.saic.grape.dao.impl..*.*(..))"
            advice-ref="txAdvice" order="1" />
        <aop:advisor pointcut="execution(* com.saic.grape.dop.dao.impl..*.*(..))"
            advice-ref="txAdvice" order="2" />
    </aop:config>
    
</beans>

<!--DAO操作-->

package com.saic.grape.dao;

import java.util.List;
import java.util.Map;

import com.saic.grape.entity.City;

/**
 *
 * @author xxxx
 * @version 1.0
 * @date
 *
 */
public interface CityDAO {

    
    /**
     * 同时插入2张表,写到事务控制范围内,保证插入时候同时成功同时失败,显示抛出异常
     * @param map
     * @return
     * @throws Exception
     */
    
    public int createTable(Map map) throws  Exception;
    
    

}


package com.saic.grape.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.saic.grape.base.dao.CommonBaseDao;
import com.saic.grape.dao.CityDAO;
import com.saic.grape.entity.City;

/**
 * 城市接口实现类
 *
 * @version 1.0
 * @date 2014-1-20
 *
 */
@Repository("cityDAO")
public class CityDAOImpl extends CommonBaseDao<City, String> implements CityDAO {
    private static final String nameSpace = "com.saic.grape.dao.CityDAO.";

    
    @Override
    public int createTable(Map map) throws Exception {
        // TODO Auto-generated method stub
        int re1= super.getSqlSession().insert(nameSpace + "createTable1", map);
        int re2= super.getSqlSession().insert(nameSpace + "createTable2", map);
        return re1+re2;
    }

}


<!--controller-->

@Controller
@RequestMapping("/home/city")
public class CityController {
    private Log logger = LogFactory.getLog(CityController.class);

    @Resource
    private CityDAO cityDAO;

/**
     * 同时插入两条记录,如果一张表操作失败,另外一张表也失败
     * @throws Exception
     */

    @RequestMapping("/getAddress/0")
    @ResponseBody
    public Object getAddress(@RequestBody Map<String, Object> params)  {
        try {
            cityDAO.createTable(params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

return null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值