Are the saved proc and program still running even though the browser is closed?

I have a .NET page that will do calculations by calling a stored process in SQL Server when the user clicks a button. It usually takes about 2 minutes to calculate, and when finished, it will update the field in the table. Now I am having a problem where the user accidentally closes the browser, the saved proc will still work and update the table. I thought the page should stop working, stop updating a field in the table, and not keep working even though the browser is closed? Any ideas on how to stop the process?

+2


source to share


4 answers


When a page request results in a call to the database, the page will wait for it to complete, but the database does not know the page information. Therefore, if the page stops waiting for any reason, the database will happily continue working until it finishes. In fact, the page request also doesn't know if the browser stays open or not, so if the user closes the browser, the page request itself will be executed before completion. Only that no one hears the result.



+3


source


Not.



The server does not know anything about whether the browser is open. The browser simply starts the process, and by the time the page loads, the interaction with the server is complete.

+3


source


You can use a covert ajax call so that the server can determine if the browser is still open and active.

To stop your stored procedure when this happens, you will need to make some significant changes to it. The only real way is to store the parameter in the database to indicate that the stored procedure should continue and then check that parameter throughout the entire stored proc. If the browser is closed, you then update this setting and you can modify the stored procedure to rollback / exit based on this value.

However, this is all quite complicated for a very small gain ... Is the problem the stored procedure keeps running?

+1


source


If you need to support cancellation of any form, you need to make a number of changes to the asp.net page and your calculations. Your calculation will have to look for the flag (possibly in the same table where it stores the result).

Then, in your code, you need to execute the procedure asynchronously. Now, to do something clean, you really need the whole page to be processed asynchronously and wake up periodically, check Request.IsClientConnected and if they are no longer connected, set the flag to cancel the computation.

This is a beautiful piece of work and it is easy to get it wrong. Also, your strategy for implementing this will vary greatly depending on whether your application needs to support 10 users or thousands (you sleep in a .Net thread pool and thus limit the scalability of your application, or have a dedicated thread to poll the IsClientConnected property and work out , which calculation to interrupt?)

0


source







All Articles