简体   繁体   English

@transactional 在服务 class 与微服务

[英]@transactional in service class with microservices

I was working under microservices and using @transactional and came across a doubt that suppose I have write @transactional over service class which internally calling different microservices for the database operations.我在微服务下工作并使用@transactional 并且遇到了一个疑问,假设我已经在服务 class 上编写了@transactional,该服务在内部为数据库操作调用了不同的微服务。 Now suppose one of the microservice fails the what will be happen to database state of other microservices as they already commit their database or how rollback will happen?现在假设其中一个微服务失败了,其他微服务的数据库 state 会发生什么,因为他们已经提交了他们的数据库,或者如何回滚?

Sample code is like示例代码就像

@Transactional
@Service
class MySerivce{
    public void method(){
        // call microservice1
        // call microservice 2 (fails or give error)
    }
}

Please share knowledge.请分享知识。 Thanks!谢谢!

Those two microservices and your application are separate systems.这两个微服务和您的应用程序是独立的系统。 @Transactional only manages the transaction local to your application. @Transactional仅管理应用程序本地的事务。 It does not know how to manage the transaction of other microservice that it calls (Also impossible to do that otherwise you will end up like a hacker to hack other microservices..)它不知道如何管理它调用的其他微服务的事务(也不可能这样做,否则你最终会像黑客一样破解其他微服务..)

So if microservice2 fails, it will not help to rollback all the things that are already done on microservice1.所以如果 microservice2 失败,回滚所有已经在 microservice1 上完成的事情也无济于事。 You have to handle it manually.您必须手动处理它。 Microservice1 needs to provide the API to undo such thing and then you need to manually catch the exception thrown by microservice1 and call this API to apply the compensation action. Microservice1 需要提供 API 来撤消此类事情,然后您需要手动捕获 microservice1 抛出的异常并调用此 API 以应用补偿动作。

A pattern called saga pattern is intended for solving such issue.一种称为 saga 模式的模式旨在解决此类问题。 You can google more about it.你可以谷歌更多关于它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM