c# - Reliable connection with Azure SQL Database -


i developing c# application storing data in azure sql database.

as know, azure sql database placed somewhere on internet. not on lan network (but question relevant reliable network lan).

i've noticed time-to-time i'm getting errors "connection closed" (or network errors). it's easy simulate clumsy. reasons errors bad network conditions.

so, first idea solve "try again". when getting error, try again , it's working good. magic.

this maybe solving problem, but, open kind of problems. not situations solution. i'll explain:

i'll separate scenarios 2 types:

  1. retry cant make damage - operation select or delete. retrying have same expected result. so, type of problems - solution working fine!
  2. insert or update - retry damage information.

i'll focus the point number 2. example, let's have:

  • a users table. columns in table: id, username, credits.
  • store procedure make user (by user id) pay of credits.

the "pay" stored procedure is:

update tblusers set [credits] -= @requestedcredits id=@id 

calling sp tricky problem:

  • if work without problem - fine.
  • if fail, don't know whether operation done on db or not. retrying here can lead user pay twice!

so, "retry" strategy here not option.

solutions i'm thought on:

i'm though solve problem adding "versionid" each row. sp now:

update tblusers set [credits] -= @requestedcredits, versionid=newid() id=@id , versionid=@oldversionid 

before making user pay(), i'll check versionid (random guid) , if guid wasn't changed after network failure while paying, i'll try again (proof data wasn't changed on db). if versionid changed, user paid service.

the problem when using multiple machines @ same time, making solution problematic. because instance maybe made pay() on version-id , i'll think change executed me (which wrong).

what do?

it sounds making sql queries local/on-premise/remote (i.e. non-azure property) sql azure database.

some of possible mechanisms of dealing

  • azure hosted data access layer api

    consider creating thin data access layer api hosted on azure webapp or vm called remote machine. api service can interact sql azure reliably.

    sql more sensitive timeout , network issues http endpoint. if queries involve transfer of large amounts of data.

  • configure increased timeout

    the database access mechanism being used c# application not specified in question. many libraries or functions data access allow specify increased timeout connection.

  • virtual private network

    azure allows you create site-to-site or point-to-site vpn better network connectivity. however, least preferred mechanism.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -