I've got a system established to lock certain content inside a database table so just one user can edit that content at any given time. Simple which part is working fine. However I am in a road block of methods to transmit a request to "unlock" this content. I've the saved procedure to unlock this content, but exactly howOrexactly where would I refer to it as once the user just shuts their browser?

Additionally you can't know once the user turns off his computer. You need to do it the other way round.

Require the lock be restored periodically. Only the site would perform the periodic renewal. When the user stops using the site, then your lock expires.

Otherwise, require user to clearly unlock this content. Other customers who wish to edit this content may then go yell in the first user once they can't do their jobs. Not really a technological solution, but nonetheless a high quality one. Shame works.

The very best factor you are able to do is add something for your Session_Finish inside your global.asax. Regrettably, this will not fire before the session occasions out.

Once the user clicks the "X" within their browser, there is not anyway to be sure the browser will be sending you anything back.

A fast note around the Session_Finish approaches. If you are using this process, then you've to make sure

  1. That sessionstate is InProc, eg. add something similar to this for your Web.config

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

  2. Make certain that you have setup IIS in order to not recycle worker processes throughout normal operation (see for example this blog post).

Edit: In a roundabout way responding to the question directly, but another approach is always to use Optimistic concurrency control around the data under consideration.

There's such event as "user shuts browser".

Nonetheless, I'm able to think about two workarounds:

  • Use Javascript/Ajax to permanently (allows say every ten seconds) call a method inside your page. The DateTime of your last query must be saved somewhere. You now write a home windows service that inspections every second which session are timed out. Perform your custom action there.
  • Make use of the global.asax Session_Finish() -Event. (can't be combined with every SessionState, lookup that ones it's functional)

Attempting to leave a stackoverflow answer page appears an "are you certainInch dialog. Possibly throughout the on-page-leave event that SO uses (or however The same is true this), you are able to send your final request by having an XmlHttpRequest object. This will not cover when the browser process shuts suddenly (use session_onend for your), however it will a minimum of send the "I am closed" event earlier

I believe your one saved process could perform the securing and unleashing (combined with "Choose @strNewMax As NewMax")...

Here's a good example from the system I've:

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 can place an archive because the next greatest bid, all while securing the whole table, so not one other bids are processed simultaneously. It'll return whether 'Y' or 'N' based on whether it labored or otherwise.

You may may take this and adjust it to suit your application.