Synchronizing the Ring Topology

The following code provides a simple example of implementing synchronization by using a ring topology. The procedure opens one of the replicas in a ring topology and attempts to synchronize this replica with the next replica in the ring. In this procedure, each replica has only one synchronization partner and synchronizes with only that partner. This code runs in each replica, so that each replica can synchronize with its partner independent of the other replicas in the set. The last replica in the list has the first replica in the list as its synchronization partner.

The example assumes that the list of replica paths and file names is stored in a table named PathsRing in each of the replicas in the ring. The PathsRing table contains the following key fields: PathID, which is the numeric identifier for each record and the primary key; and Path, which is a text field containing the path and file name of a replica.

To try this code from the sample applications, run the RingSync function from the JetBook\Samples\JetSamples.mdb database, and pass in the name of one of the databases in the JetBook\Samples\Replicas subfolder — either SampleDesignMaster.mdb or SampleReplica.mdb. Or you can import the PathsRing table into your own replicable database, and run the code from there.

Function RingSync(strDbPath As String) As Boolean
	Dim dbs As Database
	Dim rst As Recordset

	On Error GoTo Err_RingSync
	
	' Open a replica in the ring.
	Set dbs = OpenDatabase(strDbPath)
	
	' Open recordset to access path information for replicas.
	Set rst = dbs.OpenRecordset("PathsRing", dbOpenSnapshot)

	On Error Resume Next
	rst.FindFirst "[Path] = """ & dbs.Name & """"
	
	With rst
		' If passed-in database is not in the list, add it.
		If .NoMatch Then
			.AddNew
			!Path = strDbPath
			.Update
			' Move to newly created record.
			.Bookmark = .LastModified
		End If
		' Move to next record to find synchronization partner.
		.MoveNext
		' Check for end of recordset.
		If .EOF Then
			' If at end of recordset, move to first record.
			.MoveFirst
		End If
		' Synchronize passed-in database with its partner.
		dbs.Synchronize !Path
	End With

	RingSync = True

Exit_RingSync:
	On Error Resume Next
	rst.Close
	dbs.Close
	Exit Function

Err_RingSync:
	MsgBox "Error: " & Err.Number & " - " & Err.Description
	RingSync = False
	Resume Exit_RingSync
End Function

You could write this function so that if a replica’s synchronization partner cannot be found, the function opens one of the other replicas in the PathsRing table and synchronizes with it.