从 ASP.NET 页面运行长时间运行的存储过程(不需要返回)

发布于 2024-08-28 05:23:46 字数 50 浏览 5 评论 0原文

我想知道如何从页面运行存储过程,并且即使页面关闭也“让它完成”。它不需要返回任何数据。

I would like to know how you would run a stored procedure from a page and just "let it finish" even if the page is closed. It doesn't need to return any data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愛放△進行李 2024-09-04 05:23:46

以数据库为中心的选项是:

  • 创建一个表,其中包含要执行的长时间运行作业的列表(或队列)。
  • 让应用程序根据需要向队列添加一个条目。这就是它的全部作用;一旦登录并进入,就不需要维护网络会话或状态数据。
  • 将 SQL 代理作业配置为每隔 1、2、5 分钟检查一次,以查看是否有任何作业要运行。
  • 如果还有尚未启动的项目,请将最近的项目标记为已启动,然后启动它。
  • 完成后,将其标记为已完成,或者直接将其删除。
  • 检查是否有任何其他项目要运行。如果有,则重复;如果没有,则退出作业。

根据容量,您可以运行该作业的多个(名称不同)副本,同时处理列表中的项目。

(我已将此方法用于非常长时间运行的方法。这更像是一种管理类型的技巧,但它可能适合您的情况。)

A database-centric option would be:

  • Create a table that will contain a list (or queue) of long-running jobs to be performed.
  • Have the application add an entry to the queue if, when, and as desired. That's all it does; once logged and entered, no web session or state data need be maintained.
  • Have a SQL Agent job configured to check every 1, 2, 5, whatever minutes to see if there are any jobs to run.
  • If there are as-yet unstarted items, mark the most recent one as started, and start it.
  • When it's completed, mark it as completed, or just delete it
  • Check if there are any other items to run. If there are, repeat; if not, exit the job.

Depending on capacity, you could have several (differently named) copies of this job running, concurrently processing items from the list.

(I've used this method for very long-running methods. It's more an admin-type trick, but it may be appropriate for your situation.)

ヅ她的身影、若隐若现 2024-09-04 05:23:46

首先准备命令,然后将其放入线程池中。只需确保线程不依赖于任何 HTTP 上下文或任何其他 http 内部对象。如果您的请求在线程之前完成;上下文可能消失了。

Prepare the command first, then queue it in the threadpool. Just make sure the thread does not depend on any HTTP Context or any other http intrinsic object. If your request finishes before the thread; the context might be gone.

疑心病 2024-09-04 05:23:46

请参阅异步过程执行。这是即使 ASP 进程崩溃也能保证执行的唯一方法。它还可以进行自我调整,可以处理负载峰值,请求会在资源可用时排队并进行处理。

该解决方案的要点是利用 SQL Server 激活 概念,允许您在 SQL Server 的后台线程中运行存储过程,无需客户端连接

基于 SqlClient 异步方法或 CLR 线程池的解决方案不可靠,随着 ASP 进程的回收,调用会丢失,而且它们会在内存中建立请求队列,这些请求实际上会因内存消耗而触发进程回收。

基于表和代理作业的解决方案更好,因为它们可靠,但它们缺乏基于激活的解决方案的自调整功能。

See Asynchronous procedure execution. This is the only method that guarantees the execution even if the ASP process crashes. It also self tuning and can handle spikes of load, requests are queued up and processed as resources become available.

The gist of the solution is leveraging the SQL Server Activation concept, which allows you to run a stored procedure in a background thread in SQL Server without a client connection.

Solutions based on SqlClient asynch methods or on CLR thread pool are unreliable, the calls are lost as the ASP process is recycled, and besides they build up in-memory queues of requests that actually trigger a process recycle due to memory consumption.

Solutions based on tables and Agent jobs are better, as they are reliable, but they lack the self tuning of Activation based solutions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文