--- title: "Managing Reader and Writer Events in a Script" slug: "managing-reader-and-writer-events-in-a-script" updated: 2024-03-28T17:23:44Z published: 2024-03-28T17:23:44Z canonical: "skthelp.syniti.com/managing-reader-and-writer-events-in-a-script" --- > ## Documentation Index > Fetch the complete documentation index at: https://skthelp.syniti.com/llms.txt > Use this file to discover all available pages before exploring further. # Managing Reader and Writer Events in a Script Each Syniti Replicate replication is executed by two different tasks: - a Reader task which reads data from the source table in refresh or reads the source transaction log in mirroring/synchronization - a Writer task which updates the data in the target table Reader and Writer tasks are executed by a pool of threads which assign to each task a slice of execution time depending on the replication's thread priority setting. A Reader task reads data from a source table or transaction log and generates an internal Syniti Replicate record describing the data changes for a source table record. On this source record, the Reader applies the mapping defined for the replication and builds the target record which is finally inserted in a FIFO queue to be processed by the Writer task. The FIFO queue size is by default 1000 records. It can be modified by changing the setting for the Record Pool Size in the [Preferences tab](/v1/docs/replication-properties-dialog#preferences-tab) of the Replication Properties dialog. The Writer task extracts a record from the queue and translates it into an INSERT, UPDATE or DELETE SQL operation on the target table. ![](https://cdn.document360.io/ca09b4e9-1857-4c63-9f0e-1f25f166f447/Images/Documentation/Reader-writer.gif?sv=2026-02-06&spr=https&st=2026-11-17T19%3A30%3A18Z&se=2026-11-17T20%3A30%3A18Z&sr=b&sp=r&sig=TCbv91h2K8%2FWoPZgSLhnFCWUUWwKEpYKFrRYp%2Fd6ig0%3D) A record is exposed to a script function through the [IRecord](/v1/docs/irecord-interface) interface. Once the queue, or pool, has reached its maximum size (configurable in the [Replication Properties dialog](/v1/docs/replication-properties-dialog)), the Reader task waits until the Writer task has processed some records. When the queue is empty, the Writer waits until the Reader has pushed in some additional records. All the replications in a group share a single Reader task and a single Writer task. This means that a Reader/Writer task has to process data for all the source/target tables in the group. Since Reader and Writer tasks are executed by different threads, particular attention must be paid in writing scripts where some events are called by the Reader and some others by the Writer. Reader events in the order they are called: 1. [Refresh_onPrepareRefresh](/v1/docs/replication-script-events#refreshonpreparerefresh) (Refresh replications only) or [LogReader_onPrepareMirroring](/v1/docs/replication-script-events#logreaderonpreparemirroring) (Mirroring/Synchronization only) 2. [Record_onBeforeMapping](/v1/docs/replication-script-events#recordonbeforemapping) 3. [Record_onAfterMapping](/v1/docs/replication-script-events#recordonaftermapping) 4. [Replication_onConflict](/v1/docs/replication-script-eventsReplication Script Events) (Synchronization replications only) 5. [Replication_onLateConflict](/v1/docs/replication-script-events#replicationonlateconflict) (Synchronization replications only) Writer events in the order they are called: 1. [Refresh_onBeforeRefresh](/v1/docs/replication-script-events#refreshonbeforerefresh) (Refresh replications only) 2. [Refresh_onBeforeTruncate](/v1/docs/replication-script-events#refreshonbeforetruncate) (Refresh replications only) 3. [Refresh_onAfterTruncate](/v1/docs/replication-script-events#refreshonaftertruncate) (Refresh replications only) 4. [LogReader_onBeforeMirroring](/v1/docs/replication-script-events#logreaderonbeforemirroring) (Mirroring/Synchronization replications only) 5. [Record_onBeforeExecute](/v1/docs/replication-script-events#recordonbeforeexecute) 6. [Record_onExecuteError](/v1/docs/global-script-events#recordonexecuteerror) (Defined in the [global script](/v1/docs/global-script-events)) 7. [Record_onAfterExecute](/v1/docs/replication-script-events#recordonafterexecute) (Not called if record execution) 8. [LogReader_onReceiverChanged](/v1/docs/replication-script-events#logreaderonreceiverchanged) (Mirroring/Synchronization replications only) 9. [Refresh_onAfterRefresh](/v1/docs/replication-script-events#refreshonafterrefresh) (Refresh replications only) 10. [LogReader_onAfterMirroring](/v1/docs/replication-script-events#logreaderonaftermirroring) (Mirroring/Synchronization replications only) 11. [Replication_onCriticalError](/v1/docs/replication-script-events) 12. [Replication_onError](/v1/docs/global-script-events#replicationonerror) (Defined in the [global script](/v1/docs/global-script-events)) If there are objects defined in the replication script which need to be accessed and/or modified from Reader events and Writer events, then access to this object must be protected by a lock. There is no need to protect access to replication objects if they are only used in Reader events or if they are only used in Writer events. Here is an example showing how to modify a variable defined in the replication script from both the Record_onAfterMapping and Record_onAfterExecute events. The script sample uses the Reader event Record_onBeforeMapping to write a line in a text file for each record that is read from the source table, and it uses the Writer event Record_onAfterExecute to write a line in the same text file for each record inserted in the target table. Since the StreamWriter object sw, defined in the replication script, is used in both a Reader event and a Writer event, an exclusive lock must be set before using this object. ## VB.NET Example ```plaintext Imports System Imports System.IO Imports Microsoft.VisualBasic Imports DBMotoPublic Imports DBMotoScript Imports DBRS.GlobalScript Namespace DBRS     Public Class ReplicationScript : Inherits IReplicationScript        Const sFileName As String = "C:\DBMScrip.txt"        Dim sw as StreamWriter = Nothing        Public Overrides Sub Record_onBeforeMapping(recSource As IRecord, ByRef AbortRecord As Boolean)            Try              ' write the source record key into the text file               SyncLock(Me)                  If sw Is Nothing Then                     sw = New StreamWriter(sFileName)                  End If                  sw.WriteLine("Record_onBeforeMapping - Source Record: " + recSource.GetValueAfter("ID"))                  sw.Flush              End SyncLock             Catch ex As Exception                 AddLog("Exception in Record_onBeforeMapping: " + ex.ToString, 0)           End Try       End Sub ``` ```plaintext       Public Overrides Sub Record_onAfterExecute(recTarget As IRecord)           Try              ' write the target record key into the text file              SyncLock(Me)                If sw Is Nothing Then                    sw = New StreamWriter(sFileName)                End If                sw.WriteLine("Record_onAfterExecute - Target Record: " + recTarget.GetValueAfter("ID"))                sw.Flush             End SyncLock            Catch ex As Exception               AddLog("Exception in Record_onAfterExecute: " + ex.ToString, 0)         End Try       End Sub ``` ```plaintext       Public Overrides Sub Refresh_onAfterRefresh()           Try             ' close the text file after the refresh             SyncLock(Me)                If Not sw Is Nothing Then                   sw.Close                   sw = Nothing                End If             End SyncLock            Catch ex As Exception                AddLog("Exception in Refresh_onAfterRefresh: " + ex.ToString, 0)            End Try              End Sub   End Class End Namespace ``` ### C# Example ```plaintext using System; using System.Data; using DBMotoPublic; using DBMotoScript; using System.IO; namespace DBRS {    public class ReplicationScript : IReplicationScript    {       const string sFileName = "C:\\DBMScrip.txt";       StreamWriter sw = null;       public override void Refresh_onAfterRefresh()       {          lock (this)          {             // close the text file after the refresh             try               {                if (sw == null)                {                    sw.Close();                    sw = null;                }              }              catch (Exception ex)              {                 GlobalScript.AddLog("Exception in Refresh_onAfterRefresh: " + ex.ToString(), 0);              }           }        } ``` ```plaintext       public override void Record_onBeforeMapping(DBMotoPublic.IRecord recSource, ref bool AbortRecord)       {           lock (this)           {               // write the source record key into the text file               try                     {                   if (sw == null)                    {                       sw = new StreamWriter(sFileName);                       sw.WriteLine("Record_onBeforeMapping - Source Record: " + recSource.GetValueAfter("ID"));                       sw.Flush();                     }                 }                 catch (Exception ex)                 {                    GlobalScript.AddLog("Exception in Record_onBeforeMapping: " + ex.ToString(), 0}                 }              }        } ``` ```plaintext         public override void Record_onAfterExecute(DBMotoPublic.IRecord recTarget, bool Failed)        {            lock (this)            {                // write the target record key into the text file                  try                   {                   if (sw == null)                       sw = new StreamWriter(sFileName);                       sw.WriteLine("Record_onAfterExecute - Target Record: " + recTarget.GetValueAfter("ID"));                       sw.Flush();                 }                 catch (Exception ex)                 {                     GlobalScript.AddLog("Exception in Record_onAfterExecute: " + ex.ToString(), 0);                 }              }          }      }   } ``` **Related Topics** [IRecord Interface](/v1/docs/irecord-interface) [Replication Script Events](/v1/docs/replication-script-events) [Replication Script Properties](/v1/docs/replication-script-properties) [Handling Events for INSERT, UPDATE and DELETE Operations](/v1/docs/handling-events-for-insert-update-and-delete-operations) [Global Script Functions](/v1/docs/global-script-functions) [Writing a Global Script](/v1/docs/writing-a-global-script) [Writing a Replication Script](/v1/docs/writing-a-replication-script) [Writing Scripts with Visual Basic .NET](/v1/docs/writing-scripts-in-syniti-replicate) [Replication Script Editor](/v1/docs/replication-script-editor)