Correcting synchronization errors

A synchronization error happens when spMFUpdateTable, or a procedure that calls it, tries to update an object in M-Files from a version in SQL that is older than the version in M-Files. The object changed in M-Files after SQL last fetched it, so the SQL change can’t be applied safely.

When this happens, the object’s Process_ID in the class table is set to 2. What happens next depends on whether synchronization precedence is set for the class.

Causes

Synchronization errors usually point to one of these:

  • Changes are processed in SQL before the latest version of the object is fetched from M-Files.

  • Users change objects in M-Files that are meant to be updated only from SQL, and the SQL procedures don’t expect it.

Finding synchronization errors

  • In the class table: rows with Process_ID = 2. The Update_ID column on those rows identifies the update run that raised the error.

  • Across class tables: spMFClassTableStats reports a SyncError count per class table.

  • In the logs: when no precedence is set, each error is written to MFLog as “Synchronization error occured while updating ObjID …”, and the MFLog trigger sends the error email. See Error tracing.

Correcting an error

There are two ways to correct a synchronization error: by hand, or automatically by setting precedence for the class.

Manual correction

Use this path when no precedence is set, or when you want to decide object by object.

  1. Investigate the object and the root cause of the conflicting change, and correct it.

  2. Reset the object’s Process_ID to 0.

  3. Run spMFUpdateTable with @UpdateMethod = 1 to fetch the latest version from M-Files. Use @ObjIDs to limit the update to the affected objects.

  4. Redo the processing in SQL, and update M-Files again.

UPDATE dbo.MFCustomer
SET Process_ID = 0
WHERE Process_ID = 2
  AND ObjID IN (1205, 1210);

EXEC dbo.spMFUpdateTable
     @MFTableName  = N'MFCustomer',
     @UpdateMethod = 1,
     @ObjIDs       = N'1205,1210';

Automatic correction using precedence

Precedence decides which system wins when an object has changed in both M-Files and SQL. It is set per class in the SynchPrecedence column of the MFClass table:

SynchPrecedence

Precedence

What happens to an object with a synchronization error

NULL (default)

None

Process_ID stays 2, the error is written to MFLog, and nothing is corrected. Use the manual correction.

0

SQL

M-Files is updated with the values in SQL. The change made in M-Files is overwritten, and it stays in the object’s version history in M-Files.

1

M-Files

The change in SQL is discarded, and the latest version is fetched from M-Files into SQL.

When precedence is set, spMFUpdateTable applies it at the end of the same run: it calls spMFUpdateSynchronizeError for the objects that raised a synchronization error in that run. Your procedures don’t need to check for synchronization errors or call spMFUpdateSynchronizeError themselves.

Set precedence for a class:

UPDATE dbo.MFClass
SET SynchPrecedence = 1   -- NULL = none, 0 = SQL takes precedence, 1 = M-Files takes precedence
WHERE TableName = N'MFCustomer';

Correcting errors left from an earlier run

Objects that raised a synchronization error while no precedence was set keep Process_ID = 2. To correct them with precedence, set SynchPrecedence for the class, then run spMFUpdateSynchronizeError with the Update_ID of the run that raised the errors. The procedure only processes objects with Process_ID = 2 from that run, and does nothing while SynchPrecedence is NULL.

DECLARE @Update_ID INT,
        @ProcessBatch_ID INT;

SELECT @Update_ID = MAX(Update_ID)
FROM dbo.MFCustomer
WHERE Process_ID = 2;

EXEC dbo.spMFUpdateSynchronizeError
     @TableName       = 'MFCustomer',
     @Update_ID       = @Update_ID,
     @ProcessBatch_ID = @ProcessBatch_ID OUTPUT;

Repeat for each Update_ID that still has objects with Process_ID = 2.

Examples

These example scripts force a synchronization error and show the outcome for each precedence setting: