简体   繁体   English

Java同时处理多个请求

[英]Java handling multiple requests simultaneously

I have an issue where user A submits a request, we call webservice and do some validations,pick record for that user from database and send back data. 我有一个问题,用户A提交请求,我们调用webservice并做一些验证,从数据库中选择该用户的记录并发回数据。

Now, at the same time another user B submits a request, this time 2 records (user A,B records are picked from db). 现在,同时另一个用户B提交请求,这次是2条记录(用户A,B记录是从数据库中挑选出来的)。

My requirement is I want to pick only one record when user B submits the record. 我的要求是当用户B提交记录时我想只选择一条记录。 As per requirement, I cannot check against database for that particular record to pick only that one. 根据要求,我无法针对该特定记录检查数据库以仅选择该记录。 This happens only when multiple people submit at same time. 只有当多个人同时提交时才会发生这种情况。

We update a flag after sending response so that processed records are not picked up again when other request comes in. 我们在发送响应后更新标志,以便在其他请求进入时不会再次拾取已处理的记录。

The issue is, before this flag is updated if someone sends a request, then all records that has no flag is picked. 问题是,在有人发送请求之前更新此标志之前,将选择所有没有标志的记录。

Can anyone suggest how can I design this in JAVA? 任何人都可以建议我如何在JAVA中设计这个?

You should do some research on thread synchronisation. 你应该对线程同步做一些研究。

Effectively you want one interaction (or at least part of it) to be completed before you perform the next. 实际上,在执行下一个之前,您需要完成一次交互(或至少部分交互)。 Of course, if the context of a web server, you don't want one interaction to block another. 当然,如果是Web服务器的上下文,您不希望一个交互阻止另一个。 So there will be trade-offs. 所以会有权衡。

One way would be to encapsulate the reading and flagging of the records in the database into a synchronised block, so that only one thread can execute that part of the interaction at a time. 一种方法是将数据库中记录的读取和标记封装到同步块中,这样一次只有一个线程可以执行该部分交互。

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

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