I have to save records to a database and then send some data to a restful web service. I need them to happen together. If one fails then the other should not happen as well. So for example, consider the following code:
我必须将记录保存到数据库,然后将一些数据发送到一个宁静的Web服务。我需要它们一起发生。如果一个失败,那么另一个也不应该发生。例如,请考虑以下代码:
saveRecords(records);
sendToRestService(records);
If saveRecords fails with a database constraint violation then I don't want the rest call to happen. I could make saveRecords happen in it's own transaction and commit it before the call to sendToRestService but there is still the potential for the rest service to be down. I could keep up with whether the rest service succeeds and if it doesn't then try to send them later. I was just wondering if there is a better strategy since this seems like it would be a common scenario.
如果saveRecords因数据库约束违规而失败,那么我不希望其余的调用发生。我可以让saveRecords在它自己的事务中发生并在调用sendToRestService之前提交它,但是仍有可能停止其余的服务。我可以跟上休息服务是否成功,如果不成功则尝试稍后发送。我只是想知道是否有更好的策略,因为这似乎是一种常见的情况。
Thanks for any advice.
谢谢你的建议。
1 个解决方案
#1
0
why don't you try Observer design pattern?
你为什么不尝试Observer设计模式?
I'm assuming saveRecords(records) and sendToRestService(records) methods are in two different classes.
我假设saveRecords(记录)和sendToRestService(记录)方法在两个不同的类中。
If you use Observer design pattern, you can notify the class containing sendToRestService() method in case if the calling class object changes.
如果使用Observer设计模式,则可以在调用类对象发生更改时通知包含sendToRestService()方法的类。
参考:观察者设计模式
#1
0
why don't you try Observer design pattern?
你为什么不尝试Observer设计模式?
I'm assuming saveRecords(records) and sendToRestService(records) methods are in two different classes.
我假设saveRecords(记录)和sendToRestService(记录)方法在两个不同的类中。
If you use Observer design pattern, you can notify the class containing sendToRestService() method in case if the calling class object changes.
如果使用Observer设计模式,则可以在调用类对象发生更改时通知包含sendToRestService()方法的类。
参考:观察者设计模式