简体   繁体   English

在Java中处理客户端请求超时

[英]Handle client-side request timeout in java

A client sends a request and catches a timeout exception. 客户端发送请求并捕获超时异常。 However the server is still processing the request and saving it to the database. 但是,服务器仍在处理请求并将其保存到数据库。 Before that happening, the client already sent a second request which doubles the record on the database. 在此之前,客户端已经发送了第二个请求,该请求使数据库上的记录增加了一倍。 How do I prevent that from happening? 如何防止这种情况发生? Im using java servlets and javascript. 我正在使用Java Servlet和JavaScript。

A few suggestions:- 一些建议:

1) Increase the client timeout. 1)增加客户端超时时间。

2) Make the server more efficient so it can respond faster. 2)提高服务器效率,使其响应速度更快。

3) Get the server to respond with an intermediate "I'm working on it" response before returning with the main response. 3)在返回主响应之前,让服务器以中间的“我正在处理”响应进行响应。

4) Does the server need to do all the work before it responds to the client, or can some be offloaded to a seperate process for running later? 4)服务器在响应客户端之前是否需要完成所有工作,或者可以将其中的一些工作分流到单独的进程中以便以后运行?

A client sends a request and catches a timeout exception. 客户端发送请求并捕获超时异常。 However the server is still processing the request 但是服务器仍在处理请求

Make the servlet generate some output (can be just blank spaces) and flush the stream every so often (every 15 seconds for example). 使servlet生成一些输出(可以只是空白),并每隔一段时间(例如每15秒)刷新一次流。

If the connection has been closed on the client side, the write will fail with a socket exception. 如果在客户端已关闭连接,则写入将失败,并出现套接字异常。

Before that happening, the client already sent a second request which doubles the record on the database 在此之前,客户端已经发送了第二个请求,该请求使数据库上的记录翻了一番

Use the atomicity of the database, for example, a unique key. 使用数据库的原子性,例如,唯一键。 Start the process by creating a unique record (maybe in some "unfinished" status), it will fail if the record already exists. 通过创建唯一记录(可能处于某些“未完成”状态)来启动该过程,如果该记录已经存在,它将失败。

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

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