/**
 * Copyright (c) 2016-2019  All rights reserved.
 */

package com.maxbox.service;

import com.maxbox.modules.sys.entity.SysUserEntity;
import com.maxbox.datasource.annotation.DataSource;
import com.maxbox.modules.sys.dao.SysUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 测试多数据源
 *
 * @author pgh xiaopeng2260@sina.com
 */
@Service
//@DataSource("slave1")
public class DynamicDataSourceTestService {
    @Autowired
    private SysUserDao sysUserDao;

    @Transactional
    public void updateUser(Long id) {
        SysUserEntity user = new SysUserEntity();
        user.setUserId(id);
        user.setMobile("13500000000");
        sysUserDao.updateById(user);
    }

    @Transactional
    @DataSource("slave1")
    public void updateUserBySlave1(Long id) {
        SysUserEntity user = new SysUserEntity();
        user.setUserId(id);
        user.setMobile("13500000001");
        sysUserDao.updateById(user);
    }

    @DataSource("slave2")
    @Transactional
    public void updateUserBySlave2(Long id){
        SysUserEntity user = new SysUserEntity();
        user.setUserId(id);
        user.setMobile("1350000000551");
        sysUserDao.updateById(user);
        System.out.println("id5 = [" + id + "]");
//        try {
            //测试事物
            int i = 1 / 0;
//        } catch (Exception e) {
           // throw new RuntimeException("--被除数不能为0");
//            System.out.println("exception"+e.getMessage());
//        }

    }
}