i have a .NET page that will perform calculation by calling a stored proc in SQL Server whn user click on a button. Normally, the calculation takes approxiamately 2 mins to complete and upon completion it will update a field in the table. Now i have a issue that, when user accidentally close the browser, the stored proc will still be running and update the table. I thought that the page should stop running, stop updating the field in table instead of keep on running even though the browser was closed?? Any ideas how to stop the process??
views:
177answers:
4No.
The server knows nothing about whether the browser is still open. The browser just fires the process, and by the time the page is downloaded, it's interaction with the server is complete.
When a page request results in a call to the database, the page will wait for it to finish, but the database has no knowlegde of the page. So if the page stops waiting for whatever reason, the database will happily continue working until finished. In fact, the page request also has no knowlegde about whether the browser is still open or not, so if the user closes the browser, the page request itself will still execute until finished. It's only that nobody will listen to the result.
You can use a timer ajax call to allow the server to determine if the browser is still open and active.
In order to stop your stored procedure when this happens you will need to make some major modification to it. The only real way is to store a parameter in the database to indicate that the stored procedure should continue running and then check that parameter throughout your stored proc. If the browser is closed you then update this parameter and you can change your stored procedure to rollback/exit based on this value.
However, all of this is fairly complex for very little gain... Is it a problem that the stored procedure continues to run?
If you need to support some form of cancellation for this, you'll need to make a number of changes to your asp.net page and your calculation. Your calculation would have to look for a flag (maybe in the same table where it stored the result).
Then, in your code, you need to fire off the procedure execution asynchronously. Now, in order to do things cleanly, you really need the whole page to process asynchonously, and to wake periodically, check the Request.IsClientConnected, and if they're no longer connected, set the flag to cancel the calculation.
It's a fair chunk of work, and easy to get wrong. Also, your strategy for implementing this would vary wildly depending on whether your application needs to support 10 users or thousands (do you sleep in the .Net thread pool, and thus limit scalability of your application, or have a dedicated thread to poll the IsClientConnected property, and work out which calculation to abort?)