Synchronizing the Star Topology

As described in the “Replica Set Topology” section earlier in this chapter, you can implement a variety of topologies for synchronizing members of the replica set. The following code example shows how to synchronize the star topology.

The following code provides a simple example of implementing synchronization by using a star topology. The procedure opens a replica and checks to see whether it is the hub. If the replica is the hub, it synchronizes with every other replica in the star. If it is not the hub, it synchronizes with the hub only.

The example assumes that a list of replica paths and file names is stored in a table named PathsStar in each replica. The PathsStar table contains the following key fields: PathID, which is the numeric identifier for each record and the primary key; Path, which is a text field containing the path and file name of a replica; and Hub, a Boolean field that specifies the replica serving as the hub for the star.

To try this code from the sample applications, run the StarSync 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 PathsStar table into your own replicable database, and run the code from there.

Function StarSync(strDbPath As String) As Boolean
	Dim dbs As Database, dbsHub As Database
	Dim rst As Recordset

	On Error GoTo Err_StarSync
	
	' Open database.
	Set dbs = OpenDatabase(strDbPath)
	Set rst = dbs.OpenRecordset("PathsStar", dbOpenDynaset)
	
	With rst
		' Check to see whether passed-in database is listed
		' in list of replicas.
		.FindFirst "[Path] = """ & strDbPath & """"
		' If it's not, then add it to list.
		If .NoMatch Then
			.AddNew
			!Path = strDbPath
			.Update
		End If
		
		' Search table to find name of database marked as hub.
		.FindFirst "[Hub] = True"
		' If match is found, check which database is marked as hub.
		If Not .NoMatch Then
			' Open the original hub database.
			Set dbsHub = OpenDatabase(!Path)
		Else
			' If there is no hub, display error and exit.
			MsgBox "Hub does not exist in replica set. " & _
				"Can't synchronize at this time."
		End If
	End With

	' Check to see if passed-in database is the hub.
	If dbsHub.Name = dbs.Name Then
		' Synchronize all members of the replica set with the hub.
		' Since error trapping is turned off, the program continues to
		' synchronize with other replicas if the Synchronize method fails 
		' for any replica.
		 rst.MoveFirst
		Do Until rst.EOF
			' Hub can't synchronize with itself.
			If rst!Path <> dbs.Name Then
				Debug.Print rst!Path
				dbs.Synchronize rst!Path
			End If
			rst.MoveNext
		Loop
	Else
		dbs.Synchronize dbsHub.Name
	End If
	
	StarSync = True

Exit_StarSync:
	On Error Resume Next
	rst.Close
	dbs.Close
	dbsHub.Close
	Exit Function

Err_StarSync:
	' If an error occurred while synchronizing with the current
	' replica, log an error that stores which replica failed to
	' synchronize. The LogError function is a custom procedure
	' for logging errors to a text file.
	If Err.Number <> 0 Then
		' If recordset is not open, error is not a synchronization error.
		If rst Is Nothing Then
			MsgBox "Error: " & Err.Number & " - " & Err.Description
			StarSync = False
			Resume Exit_StarSync
		Else
			LogError Err, rst!Path
			MsgBox ("Synchronization failure at " & rst!Path)
			Err.Clear
			Resume Next
		End If
	End If
End Function 

If an error occurs during synchronization, the following function is called to log the error:

Function LogError(errX As ErrObject, Optional strPath As String)
	Dim intX As Integer
	
	intX = FreeFile(0)
	Open "ErrLog.txt" For Append As #intX
	Write #intX, strPath
	Write #intX, errX.Number
	Write #intX, errX.Description
	Write #intX, Now
	Write #intX,
	Close #intX
End Function