简体   繁体   English

如何在一个应用程序中处理多个同时出现的用户请求

[英]how to handle multiple simultaneous user requests in an application

I am creating an application in Java in which there is a table in database which stores available access slots. 我正在用Java创建一个应用程序,其中数据库中有一个表存储可用的访问插槽。

Basically when a user makes a request, the program should find a record in table which has an open(unused) slot. 基本上,当用户发出请求时,程序应该在表中找到一个具有打开(未使用)插槽的记录。 After the user's data is sent to him, that slot is marked as used and cannot be used anymore. 在将用户的数据发送给他之后,该插槽被标记为已使用,无法再使用。

What I am confused about is, how to handle many simultaneous requests... For eg, if 2 requests come in at the same time, then isnt there a possibility of both of them picking up the same slot(record) from the table? 我感到困惑的是,如何处理许多同时发生的请求...例如,如果同时有2个请求,那么这两个请求是否都可能从表中拾取相同的插槽(记录)? How do I ensure that even though there are many simultaneous requests, each request picks a unique unused slot, and that all requests pick different unused slots. 我如何确保即使同时有多个请求,每个请求也会选择一个唯一的未使用插槽,并且所有请求都将选择不同的未使用插槽。

One thing more, I do have many many slots, but it is important that no single slot be picked up by 2 different requests. 还有一点,我确实有很多插槽,但是重要的是两个不同的请求都不能占用一个插槽。 However even this may change in future, if the number of requests rises tremendously...So I need a solution that is built to handle a huge number of requests in the manner that i have described. 但是,即使将来这种情况可能会改变,如果请求的数量急剧增加……所以我需要一种能够以我所描述的方式处理大量请求的解决方案。

What you want to do is called Connection pooling (example using tomcat here). 您要执行的操作称为“ 连接池” (在此处使用tomcat的示例)。 The solution for the mentioned problem would be to use the semaphoreing system that a database allows, specifically transactions. 解决上述问题的方法是使用数据库允许的信号量系统,特别是事务。

First, you read the table and find the first record that is unused. 首先,您读取表并找到第一个未使用的记录。 You propagate the number back to your application. 您将数字传播回您的应用程序。 Then you try to open the record exclusively (for writing). 然后,您尝试以独占方式打开记录(用于写入)。 You check again if the record is still unused, and if it is, fetch the data for the record. 您再次检查记录是否仍未使用,如果是,则获取该记录的数据。 Save the data to the record and release it. 将数据保存到记录中并释放它。 If, however, the record is suddenly used when you open it for writing, you need to fall back and look for a new record again and repeat the mechanism. 但是,如果在打开该记录进行写入时突然使用了该记录,则需要后退并再次寻找新记录并重复该机制。

You put your slot pickup requests in a synchronized queue. 您将插槽提取请求放入同步队列中。 This way you will guarantee that slot pickup is FIFO (first in first out). 这样,您将确保插槽接收是FIFO(先进先出)。 You can use this 你可以用这个

What DB are you using? 您正在使用哪个数据库? MySQL has SELECT ... FOR UDPATE row locking support. MySQL具有SELECT ... FOR UDPATE行锁定支持。

Try this, 尝试这个,

Use ArrayBlockingQueue for thread safe access.... This will make sure that only one thread can access the record at a time. 使用ArrayBlockingQueue进行线程安全访问。...这将确保一次仅一个线程可以访问记录。

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

相关问题 如何为多个并发事务请求正确处理 JPA ObjectOptimisticLockException? - How to handle JPA ObjectOptimisticLockException properly for multiple simultaneous transaction requests? 如何编程以处理来自不同客户端的多个同时请求? - How to program to handle multiple simultaneous requests from different clients? 如何在 Java Server 中处理许多同时发生的 Web 请求? - How to handle few of many simultaneous Web requests in Java Server? 为多个同时的SOAP请求配置Tomcat - Configure Tomcat for multiple simultaneous SOAP requests 编写Java服务器以处理多个并发客户端 - Writing Java server to handle multiple simultaneous clients 如何使用Java处理mysql中的并发事务? - How to handle simultaneous transactions in mysql with Java? 集群式无状态Web应用程序-如何处理并发请求? - Clustered, Stateless web application - how to handle concurrent requests? 单个 servlet 如何处理来自客户端的多个请求 - How does a single servlet handle multiple requests from client side Spring MVC控制器如何处理多个长HTTP请求? - How Spring MVC controller handle multiple long http requests? 如何处理多个servlet请求以更新DB值 - How to handle multiple servlet requests to update DB value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM