sping多数据源切换之事务支持回滚

 

package gomehigo.pc.web.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gomehigo.cacheClient.service.ICacheClient;
import com.gomehigo.pc.dao.CmsCategoryMapper;
import com.gomehigo.pc.dao.UserinfoMapper;
import com.gomehigo.pc.pojo.CmsCategory;
import com.gomehigo.pc.pojo.CmsCategoryExample;
import com.gomehigo.pc.pojo.Userinfo;
import gomehigo.pc.web.datasource.DataSource;
import gomehigo.pc.web.exception.BusinessException;
import gomehigo.pc.web.service.inter.DataSourceService;
import gomehigo.pc.web.service.inter.TestService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xiaotian on 2017/3/6.
 */
@Service
public class TestServiceImpl implements TestService {

    /**
     *
     * 在A方法中执行B方法 A方法是执行读库 B方法执行写库
     * 由于Aop的动态代理机制只是对类进行增强 而不是扩展类 不能 当执行A方法中的B
     * 方法时,即使B方法上有注解DataSource apo也不能知道A方法中执行的是什么
     * aop只是对A方法进行了增强  所以要手动注册bean 这样才能对类方法进行增强
     * that's all
     *
     */
//对类进行重新初始化 这样更新操作就支持事务了
@Resource(name = "testServiceImpl") private TestService self; //dao层 @Autowired private CmsCategoryMapper cmsCategoryMapper; //缓存客户端 @Resource(name="cacheClient") private ICacheClient cacheClient; //分类缓存key @Value("${GOMEHIGO_PC_CAT_INFO}") private String PC_CAT_CACHE_INFO; //缓存key最底层 @Value("${GOMEHIGO_PC_BASE}") private String GOMEHIGO_PC_BASE; @Resource(name = "userinfoMapper") private UserinfoMapper mapper; @Resource(name = "dataSourceServiceImpl") private DataSourceService dataSourceService; /** * 查询 * * @param param * @return */ @Override public List<CmsCategory> findCategoryListByCondition(String param) throws BusinessException { //缓存key String cacheKey = PC_CAT_CACHE_INFO + "111" + GOMEHIGO_PC_BASE; //获取缓存中的数据 String cacheJson = cacheClient.get(cacheKey); if (StringUtils.isNotEmpty(cacheJson)) { List<CmsCategory> catList = JSONObject.parseObject(cacheJson, List.class); return catList; } //做必要的try catch处理 事务会滚处理 //查询数据库 CmsCategoryExample example = new CmsCategoryExample(); CmsCategoryExample.Criteria criteria = example.createCriteria(); // criteria.andDelFlagEqualTo(0); List<CmsCategory> categoryList = cmsCategoryMapper.selectByExample(example); //设置缓存 cacheClient.set(cacheKey, JSONObject.toJSONString(categoryList)); cacheClient.expire(cacheKey, 180); return categoryList; } @Override @DataSource("write") public int updateUser(Userinfo userinfo) { //Userinfo userinfo = new Userinfo(); int i1 = mapper.updateByPrimaryKeySelective(userinfo); int i=1/0; return i1; } /** * 事务传播行为类型 说明 PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是 最常见的选择。 PROPAGATION_SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。 PROPAGATION_MANDATORY 使用当前的事务,如果当前没有事务,就抛出异常。 PROPAGATION_REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。 PROPAGATION_NOT_SUPPORTED 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 PROPAGATION_NEVER 以非事务方式执行,如果当前存在事务,则抛出异常。 PROPAGATION_NESTED 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与 PROPAGATION_REQUIRED 类似的操作。 跨库需要配置 PROPAGATION_REQUIRES_NEW */ @Override @DataSource("read") public void dataSourceTest() throws BusinessException { //做必要的try catch处理 事务会滚处理 //查询数据库 CmsCategoryExample example = new CmsCategoryExample(); CmsCategoryExample.Criteria criteria = example.createCriteria(); // criteria.andDelFlagEqualTo(0); List<CmsCategory> categoryList = cmsCategoryMapper.selectByExample(example); //System.out.println("count:"+count); CmsCategory category = new CmsCategory(); category.setCmsCateId(35); category.setCmsCateName("一级分类ddddm"); cmsCategoryMapper.updateByPrimaryKeySelective(category); Userinfo userinfo = new Userinfo(); userinfo.setId(1); userinfo.setUsername("1111111"); int count = self.updateUser(userinfo); //mapper.updateByPrimaryKeySelective(userinfo); System.out.println("count:"+count); // int i=1/0; } }

 

posted @ 2017-11-07 20:58  霄九天  阅读(2510)  评论(0编辑  收藏  举报