Browse DevX
Sign up for e-mail newsletters from DevX


Synchronize Your Databases with .NET Web Services (Part II)  : Page 9

When data gets created in many locations, you often need to create a process that collects and copies this data to multiple sites. In this article, you'll see how to use Web services to automatically synchronize remote databases in a decentralized way, by letting each machine query the others until they all contain the same data.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Committing or Rolling Back the Transaction
If the update succeeds, you can move on to repeat the process for the next table. If an error occurs, the application throws an exception that halts execution, calling the Rollback method of the current transaction first so that no updates will be applied to any of the tables. However, after successfully processing all the tables in the list, you can safely attempt to commit the transaction. The enclosing Try...Catch construct also rolls back all updates if any other error occurs. The Finally section ensures that the connection gets closed when the process completes (it also executes if an error occurs, before raising the exception to the calling routine).

... Catch e As Exception ' error encountered so roll back all the updates oTrans.Rollback() Throw New Exception( _ "Error updating target table " _ & sTableName & " - " & e.Message) End Try End If End If Next ' process next table in list oTrans.Commit() ' all OK so commit all of the updates Catch e As Exception ' error encountered so roll back all the updates oTrans.Rollback() ' and stop, though could just write error message ' and process next URL Throw New Exception( _ "Transaction failed to complete - " & e.Message) Finally oConn.Close() End Try ...

Updating the "Last Updated" File
The ProcessAllSynchOperations routine ends by calling a separate routine named UpdateLastDateLoaded, which just writes the current date and time into the "last updated" file for this Web service URL (you'll recall that this is a file with a name such as 217_45_1_242.txt). Then the code returns to process the next Web service URL in the list:

... ' update last synchronization date file for this site UpdateLastDateLoaded(sWebSiteIP) Next ' process next site (next Web service URL) End Sub

The synchronization is complete for this client after it processes all the Web service URLs in the configuration list. Meanwhile, all the other machines within the synchronization group will carry out the same process, fetching new rows from each other and adding them to their local databases. You should schedule each machine for a slightly different time, so that they aren't all trying to synchronize at the same time.

Thanks for your registration, follow us on our social networks to keep up-to-date