views:

104

answers:

2

I have a C# ASP.NET web app running on two machines behind a load balancer. We use sticky sessions so we do not have a shared out of process session state. Our code makes use of a SQL database and we process credit cards.

We need to synchronize execution of a block of code that checks conditions in the database and also attempts to charge a credit card. Checking the conditions and executing the credit card transaction must take place in the same synchronized block.

The only solution we have come up with is to move this block of code into a web service and call that web service from the machines. This presents two issues..

1) Fairly complicated data must be managed and returned by the web services 2) The web service will become another point of failure that can't be made redundant.

Any tools or frameworks out there to help synchronize code running on different machines?

A: 

Wouldn't it be sufficent to put explictit locks on the database rows involved?

Daniel Brückner
A: 

Use an application lock: sp_getapplock

Lock your 'resource' in appropriate mode (X) for the appropriate length (from start of condition check until end of your inserts).

Remus Rusanu