简体   繁体   English

从用户到Spring MVC Controller方法的并发请求中的数据完整性

[英]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.

相关问题 配置Spring MVC以将GET请求映射到控制器中的一种方法,将OPTIONS请求映射到另一种方法 - Configuring Spring MVC to map GET requests to one method in a controller and OPTIONS requests to another method Spring MVC可以注入示例吗 <T> 从spring数据到控制器方法调用&gt; - Can Spring MVC inject Example<T> from spring-data into controller method invocation > 从Spring MVC控制器发出HTTP请求以更新div内容? - Asynch http requests from spring mvc controller to update div contents? Spring MVC - 仅允许来自本地主机的请求到特定控制器 - Spring MVC - allowing requests from localhost only to specific controller 如何从 Spring MVC\\Boot Controller 方法中正确检索登录的用户信息? - How to correctly retrieve the logged user information from a Spring MVC\Boot Controller method? 在MVC Spring中将数据从视图传递到控制器 - passing data from view to controller in mvc spring Java在Spring MVC中防止并发访问方法 - Java Preventing concurrent access to a method in Spring MVC 将复杂的数据从angularjs控制器传递到Spring MVC控制器 - pass complex data from angularjs controller to Spring MVC controller Spring MVC:未调用控制器方法 - Spring mvc: Controller method is not invoked Spring MVC @Controller方法签名 - Spring MVC @Controller method signatures
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM