防止数据库更新期间会话超时
背景
Web 应用程序调用存储过程来执行密集的数据库更新。 web.xml
的相关部分更新为四个小时:
<session-config>
<session-timeout>240</session-timeout>
</session-config>
该解决方案可用的技术包括 Java 1.4.2、Struts 2、Tomcat 5.5 和 Apache commons。大多数其他技术(例如 jQuery)都是不允许的。
问题
更新需要大约一个小时才能运行,但是四个小时的配置值违反了公司标准(有充分的理由)。生产中不允许使用四小时超时配置。
问题
如何确保执行数据库更新时请求不会超时?
想法
对于前两种情况,我担心的是生成的进程最终会被 Servlet 容器杀死。
页面刷新
- 将数据库更新过程作为后台任务生成。
- 让 Servlet 不断刷新页面以检查是否完成。
JavaScript Ping
- 将数据库更新过程作为后台任务生成。
- 让 JavaScript 代码对服务器执行 ping 操作一段时间。
类似于 在 JSF 中防止长时间处理期间会话超时,但没有 jQuery。
更新服务器
编写一个简单的服务器来侦听请求:
- Servlet 向侦听器发送请求。
- 侦听器运行更新。
由于服务器独立于Tomcat运行,因此不会发生会话超时。数据库更新将运行至完成而不会被终止。这有很多问题(错误处理不是我最关心的),并且可能是最后的选择。
优化
优化查询以在 30 分钟(允许的最大超时)内完成是可能的,但查询可能无法充分优化。
硬件
不幸的是,升级数据库硬件不是一个选择。
非常感谢!
Background
A web application calls a stored procedure to perform an intensive database update. The relevant portion of web.xml
was updated to four hours:
<session-config>
<session-timeout>240</session-timeout>
</session-config>
The technologies available for the solution include Java 1.4.2, Struts 2, Tomcat 5.5, and Apache commons. Most other technologies (such as jQuery) are not permitted.
Problem
The update takes about an hour to run, however a configuration value of four hours is against corporate standards (for good reason). A four hour timeout configuration is not permitted in production.
Question
What will ensure that the request does not time out while the database update executes?
Ideas
My concern in the first two cases is that the spawned process will eventually be killed by Servlet container.
Page Refresh
- Spawn the database update process as a background task.
- Have a Servlet continually refresh the page to check for completion.
JavaScript Ping
- Spawn the database update process as a background task.
- Have JavaScript code ping the server for a while.
Similar to Preventing session timeout during long processing time in JSF, but without jQuery.
Update Server
Write a simple server that listens for requests:
- The Servlet sends a request to the listener.
- The listener runs the update.
Since the server runs independently of Tomcat, the session timeout cannot occur. The database update will run to completion without being killed. This has numerous issues (error handling not the least of my concern) and is likely the option of last resort.
Optimization
Optimizing the query to finish in under 30 minutes (the maximum permitted timeout) is possible, but it is likely the query cannot be optimized sufficiently.
Hardware
Upgrading the database hardware is not an option, unfortunately.
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,没有用户愿意坐在屏幕前监视后台作业 4 个小时。几年前,我不得不花费数小时来实现报告生成。实施的解决方案如下:
In my opinion, no user would want to sit in front of screen monitoring a background job for 4 hours. Few years ago, I had to implement report generation which took hours. the implemented solution was the following:
通过阅读 abvoe,我可以确保您有两个选择,尽管第二个很困难,但它是最好的过程
1) 页面刷新
2) 优化
优化查询以在 30 分钟(允许的最大超时)内完成是可能的,但查询可能无法充分优化。
By reading the abvoe , i can ensure you have two options even though second one is difficult, its the best proces
1) Page Refresh
2) Optimization
Optimizing the query to finish in under 30 minutes (the maximum permitted timeout) is possible, but it is likely the query cannot be optimized sufficiently.