Solon v3.0.0

二、事务的注解与手动使用示例

</> markdown

1、注解控制模式

示例1:父回滚,子回滚(最常用的策略)

@Component
public class UserService{
    @Tran
    public void addUser(UserModel user){
        //....
    }
}

@Controller
public class DemoController{
    @Inject
    UserService userService; 
    
    //父回滚,子回滚
    //
    @Tran
    @Mapping("/user/add2")
    pubblic void addUser2(UserModel user){
        userService.addUser(user); 
        throw new RuntimeException("不让你加");
    }
}

示例2:父回滚,子不回滚

@Component
public class UserService{
    @Tran(policy = TranPolicy.requires_new)
    public void addUser(UserModel user){
        //....
    }
}

@Controller
public class DemoController{
    @Inject
    UserService userService; 
    
    //父回滚,子不回滚
    //
    @Tran
    @Mapping("/user/add2")
    pubblic void addUser2(UserModel user){
        userService.addUser(user); 
        throw new RuntimeException("不让你加;但还是加了:(");
    }
}

示例3:子回滚父不回滚

@Component
public class UserService{
    @Tran(policy = TranPolicy.nested)
    public void addUser(UserModel user){
        //....
        throw new RuntimeException("不让你加");
    }
}

@Controller
public class DemoController{
    @Inject
    UserService userService; 
    
    //子回滚父不回滚
    //
    @Tran
    @Mapping("/user/add2")
    pubblic void addUser2(UserModel user){
        try{
            userService.addUser(user); 
        }catch(ex){ }
    }
}

示例4:多数据源事务示例

@Component
public class UserService{
    @Db("db1")
    UserMapper userDao;
    
    @Tran
    public void addUser(UserModel user){
        userDao.insert(user);
    }
}

@Component
public class AccountService{
    @Db("db2")
    AccountMappeer accountDao;

    @Tran
    public void addAccount(UserModel user){
        accountDao.insert(user);
    }
}

@Controller
public class DemoController{
    @Inject
    AccountService accountService; 
    
    @Inject
    UserService userService; 
    
    @Tran
    @Mapping("/user/add")
    public void addUser(UserModel user){
        userService.addUser(user);     //会执行db1事务
        
        accountService.addAccount(user);    //会执行db2事务
    }
}

2、手动控制模式

示例1:父回滚,子回滚

@Component
public class UserService{
    public void addUser(UserModel user){
        TranUtils.execute(new TranAnno(), ()->{
            //...
        });
    }
}

@Controller
public class DemoController{
    @Inject
    UserService userService; 
    
    //父回滚,子回滚
    //
    @Mapping("/user/add2")
    pubblic void addUser2(UserModel user){
        TranUtils.execute(new TranAnno(), ()->{
            userService.addUser(user); 
            throw new RuntimeException("不让你加");
        });
    }
}

示例2:父回滚,子不回滚

@Component
public class UserService{
    
    public void addUser(UserModel user)  throws Throwable{
        TranUtils.execute(new TranAnno().policy(TranPolicy.requires_new), ()->{
            //...
        });
    }
}

@Controller
public class DemoController{
    @Inject
    UserService userService; 
    
    //父回滚,子不回滚
    //
    @Mapping("/user/add2")
    pubblic void addUser2(UserModel user){
        TranUtils.execute(new TranAnno(), ()->{
            userService.addUser(user); 
            throw new RuntimeException("不让你加;但还是加了:(");
        });
    }
}