Question

I need to sync(upload first to remote DB-download to mobile device next) DB tables with remote DB from mobile device (which may insert/update/delete rows from multiple tables).

The remote DB performs other operation based on uploaded sync data.When sync continues to download data to mobile device the remote DB still performing the previous tasks and leads to sync fail. something like 'critical condition' where both 'sync and DB-operations' want access remote Databse. How to solve this issue? is it possible to do sync DB and operate on same DB at a time?

Am using Sql server 2008 DB and mobilink sync.

Edit: Operations i do in sequence:

1.A iPhone loaded with application which uses mobilink for SYNC data.

2.SYNC means UPLOAD(from device to Remote DB)followed by DOWNLOAD(from Remote DB to device).

3.Remote DB means Consolidated DB ; device Db is Ultralite DB.

4.Remote DB has some triggers to fire when certain tables are updated.

5.An UPLOAD from device to Remote will fire triggers when sync upload finished.

6.Very next moment the UPLOAD finished DOWNLOAD to device starts.

7.Exactly same moment those DB triggers will fire.

8.Now a deadlock between DB SYNC(-DOWNLOAD) and trigger(Update queries included within) operations occur.

9.Sync fails with error saying cannot access some tables.

Was it helpful?

Solution

I did a lots of work around and Google! Came out with a simple(?!) solution for the problem. (though the exact problem cannot be solved at this point ..i tried my best).

  1. Keep track of all clients who does a sync(kind of user details).
  2. Create a sql job scheduler which contains all the operations to be performed when user syncs.
  3. Announce a "maintenance period" everyday to execute the tasks of sql job with respect to saved user/client sync details.
  4. Here keeping track of client details every time is costlier but much needed!
  5. Remote consolidated DB "completely-updated" only after maintenance period.

Any approaches better than this would be appreciated! all Suggestions are welcome!

OTHER TIPS

My understanding of your system is following:

  1. Mobile application sends UPDATE statement to SQL Server DB.
  2. There is ON UPDATE trigger, that updates around 30 tables (= at least 30 UPDATE statements in the trigger + 1 main update statement)
  3. UPDATEis executed in single transaction. This transaction ends when Trigger completes all updates.
  4. Mobile application does not wait for UPDATE to finish and sends multiple SELECT statements to get data from database.
  5. These SELECTstatements query same tables as the Trigger above is updating.
  6. Blocking and deadlocks occur at some query for some user as Trigger is not completing updates before selects and keeps lock on tables.

When optimizing we are trying make it our processes less easy for computer, achieve same result in less iterations and use less resources or those resources that are more available/less overloaded.

My suggestions for your design:

  1. Use parametrized SPs. Every time SQL Server receives any statement it creates Execution plan. For 1 UPDATE statement with a trigger DB needs at least 31 execution plan. It happens on busy Production environment for every connection every time app updates DB. It is a big waste.

  2. How SPs would help reduce blocking? Now you have 1 transaction for 31 queries, where locks are issued against all tables involved and held until transaction commits. With SP you'll have 31 small transaction and only 1-2 tables will be locked at a time.

Another question I would like to address: how to do asynchronous updates to your database? There is a feature in SQL Server called Service Broker. It allows to process message queue (rows from the queue table) automatically: it monitors queue, takes messages from it and does processing you specify and deletes processes messages from the queue.

For example, you save parameters for your SPs - messages - and Service Broker executes SP with parameters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top