[英]Data integrity in concurrent requests from a user to a Spring MVC Controller method
I was hoping to understand how one can ensure data integrity in case of concurrent requests from same user to the same Spring Controller method ? 我希望了解在同一用户对同一Spring Controller方法的并发请求的情况下如何确保数据完整性?
for egsuppose in an Online shopping scenario,a user happens to make concurrent requests to a Controller method (eg '/debitWallet?amount=100' to deduct his wallet money). 例如,假设在一个在线购物场景中,用户碰巧同时向Controller方法发出请求(例如'/ debitWallet?amount = 100'扣除他的钱包里的钱)。 The 'Wallet' could be a hibernate entity which is obtained in this method through a standard 'WalletService' instance -->'WalletDao' instance. “ Wallet”可以是通过标准“ WalletService”实例->“ WalletDao”实例在此方法中获得的休眠实体。 Now how can we ensure data integrity of the wallet for concurrent requests? 现在我们如何确保并发请求的钱包数据完整性?
- On what objects do I synchronize here? 我要在哪些对象上同步?
- what would be the scope of different beans(service,dao etc.) although I don't see any way that would help since the Wallet is going to be taken from the data store? 不同的bean(服务,dao等)的范围是什么?尽管由于将钱包从数据存储中取出,我看不出有任何帮助的方法?
- Should I even take the Wallet from the DB every-time the Controller method is invoked.Would it be a right approach? 我是否应该每次调用Controller方法时都从数据库中获取电子钱包,这是正确的方法吗? Instead should I use @SessionAttribute on this Wallet entity & then use it for every request to this method? 相反,我应该在此Wallet实体上使用@SessionAttribute,然后将其用于对此方法的每个请求吗?
I could really use some help here to understand how to tackle data-integrity issue in this use case? 我真的可以在这里使用一些帮助来了解如何在此用例中解决数据完整性问题?
First of all answer the question: how frequent will be your data changed? 首先回答以下问题:您的数据多久更改一次?
If it is not so frequent (or your database iterations are fast) you can use pattern: "User always operates with recent wallet instance, which is constantly synchronized with database". 如果不是那么频繁(或者您的数据库迭代很快),则可以使用模式:“用户始终使用最近与电子钱包保持同步的钱包实例进行操作”。 And to make it work user always sends Optimistick lock value (@Version field on entity), and in case changes happened in background: user receives Optimistick locking exception. 为了使其正常工作,用户总是发送Optimistick锁定值(实体上的@Version字段),并且如果后台发生了更改:用户会收到Optimistick锁定异常。
If it is frequent you should deeply analyze your implementation and then - search places for synchronization. 如果经常使用,则应深入分析实现,然后-搜索同步位置。 Or even rework your API. 甚至重新设计您的API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.