springboot 事务自调用失效


springboot 事务自调用失效

 

产生原因:spring事务底层使用动态代理,若同一个类中的方法吊用另一个方法,则不存在对象之间的代理关系,被调用方法的事务失效

解决方法:在另一个类中调用需要调用的方法

 

*************************

示  例

 

*****************

service 层

 

public interface CustomPersonService {

    void update(Person person);

    void insert(Person person);

    void insertList(List personList);
}


*********************

serviceImpl 层

 

@Service
public class CustomPersonServiceImpl implements CustomPersonService {

    @Autowired
    private PersonMapper personMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void update(Person person) {
        personMapper.updateById(person);
    }

    @Override
    @Transactional(propagation = Propagation.NESTED)
    public void insert(Person person) {
        personMapper.insert(person);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void insertList(List personList) {
        for(Person person:personList){
            insert(person);
        }
    }
}

 

*******************

controller 层

 

@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private PersonService personService;

    @Autowired
    private CustomPersonService customPersonService;

    @RequestMapping("/save")
    public String save(){
        List list=new ArrayList<>();
        for(int i=0;i<10;i++){
            Person person=new Person();
            person.setName("瓜田李下"+i);
            person.setAge(i);

            list.add(person);
        }

        customPersonService.insertList(list);

        return "success";
    }
}

 

*********************

控制台日志输出

 

2019-09-26 10:56:46.156 DEBUG 13792 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : GET "/person/save", parameters={}
2019-09-26 10:56:46.157 DEBUG 13792 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public java.lang.String com.example.demo.controller.PersonController.save()
2019-09-26 10:56:46.157 DEBUG 13792 --- [nio-8080-exec-4] o.s.j.d.DataSourceTransactionManager     : Creating new transaction with name [com.example.demo.serviceImpl.CustomPersonServiceImpl.insertList]: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT
2019-09-26 10:56:46.530 DEBUG 13792 --- [nio-8080-exec-4] o.s.j.d.DataSourceTransactionManager     : Acquired Connection [com.mysql.cj.jdbc.ConnectionImpl@976ff06] for JDBC transaction
2019-09-26 10:56:46.530 DEBUG 13792 --- [nio-8080-exec-4] o.s.j.d.DataSourceTransactionManager     : Switching JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@976ff06] to manual commit
2019-09-26 10:56:46.599 DEBUG 13792 --- [nio-8080-exec-4] o.s.j.d.DataSourceTransactionManager     : Initiating transaction commit
2019-09-26 10:56:46.599 DEBUG 13792 --- [nio-8080-exec-4] o.s.j.d.DataSourceTransactionManager     : Committing JDBC transaction on Connection [com.mysql.cj.jdbc.ConnectionImpl@976ff06]
2019-09-26 10:56:46.683 DEBUG 13792 --- [nio-8080-exec-4] o.s.j.d.DataSourceTransactionManager     : Releasing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@976ff06] after transaction
2019-09-26 10:56:46.686 DEBUG 13792 --- [nio-8080-exec-4] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json]
2019-09-26 10:56:46.687 DEBUG 13792 --- [nio-8080-exec-4] m.m.a.RequestResponseBodyMethodProcessor : Writing ["success"]
2019-09-26 10:56:46.689 DEBUG 13792 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

可以看出,被调用方法的事务没有创建

 

 

你可能感兴趣的:(事务)