Make the final database call when the user leaves the website (ASPX)?

I have a system designed to lock certain content in a database table, so only one user can edit this content at a time. Easy and this part works fine. But now I'm in the road block on how to send a request to “unlock” content. I have a stored procedure for unlocking content, but how / where can I call it when the user just closes his browser?

+4
source share
6 answers

You also cannot know when a user shuts down his computer. You should do it the other way around.

It is necessary to periodically update the castle. Only the website will perform periodic updates. If the user stops using the website, the blocking period expires.

Otherwise, the user is required to explicitly unlock the content. Other users who want to edit the content can then shout to the first user when they cannot do their job. Not a technological solution, but still good. Shame works.

+5
source

The best thing you can really do is add something to your Session_End in your global.asax. Unfortunately, this will not work until the session ends.

When a user clicks the “X” button in his browser, it is in any case not guaranteed that the browser will send you anything back.

+1
source

A quick note on Session_End approaches. If you use this method, you need to provide

  • This session state is InProc, for example. add something like this to your web.config

    <sessionState mode = "InProc" timeout = "timeout_in_minutes" />

  • Make sure you configure IIS to not overwork workflows during normal operation (see, for example, this blog post ).

Edit: Not directly answering the question directly, but another approach is to use Optimistic concurrency control for the data in question.

+1
source

There is such an event as "the user closes the browser."

However, I can think of two workarounds:

  • To use Javascript / Ajax on an ongoing basis (say, every 10 seconds), call the method on your page. DateTime of your last request needs to be saved somewhere. Now you are writing a windows service that checks every second which session is completed. perform your own action there.
  • Use the global.asax Session_End () -Event command. (cannot be used with every SessionState, find which ones you can use)
0
source

Attempting to leave the stackoverflow response page displays the "are you sure" dialog box. Perhaps during an on-page-leave event that uses SO (or nonetheless SO does), you can send the final request with an XmlHttpRequest object. This will not be displayed if the browser process terminates unexpectedly (use session_onend session for this), but it will at least send the "I am closed" event earlier

0
source

I think your one stored procedure can do lock and unlock (used with "Select @strNewMax As NewMax") ...

Here is an example from the system:

Declare @strNewMax Char Select @strNewMax = 'N' BEGIN TRANSACTION /* Lock only the rows for this Item ID, and hold those locks throughout the transaction. */ If @BidAmount > (Select Max(AB_Bid_AMT) from AuctionBid With(updlock, holdlock) Where AB_AI_ID = @AuctionItemId) Begin Insert Into AuctionBid (AB_AI_ID, AB_Bid_AMT, AB_Emp_ID, AB_Entry_DTM) Select @AuctionItemId, @BidAmount, @EmployeeId, GetDate() Select @strNewMax = 'Y' End COMMIT TRANSACTION Select @strNewMax As NewMax 

This will insert the record as the next highest bid, with the entire blocking of the entire table, so that other bets will not be processed at the same time. It will return either "Y" or "N" depending on whether it works or not.

Perhaps you can accept this and customize it according to your application.

0
source

Source: https://habr.com/ru/post/1303972/


All Articles