Flush
Flush
flushes the write buffer, which is filled by the Writeb instruction.
Flush [CLASS]
CLASS
is the abbreviation of the table in which the flush is done. By default, the current table as defined by File or by DefaultFile is considered.# First example : let's create a set of account. fstat will be not null if an error occuredLocal File ACCOUNTS [ACC]Trbegin [ACC]For I=1 to dim([L]ACCOUNT_LIST)[ACC]ACCOUNT=[L]ACCOUNT_LIST(I)[ACC]TYPE=1[ACC]DESCRIPTION="Account"-num(I)Writeb [ACC]Break (fstat<>0)Next IIf fstat=0Flush [ACC] : # Mandatory before Commit, otherwise the Commit would raise an error if lines not flushedIf fstat=0CommitElseRollbackEndifElseRollbackEndif# Second example: a subprogram that creates a set of records given by an array of instancesSubprog CREATE_RECORD(VALUES,NUMBER,RETURN_MESSAGE)Variable Instance VALUES(1..) Using C_RECORDValue Integer NUMBERVariable Char RETURN_MESSAGELocal File MYTABLE [MYT]Local Integer ITrbegin [MYT]# Let's create the lines with a buffer value of 10[S]adxwrb=10Gosub CREATE_RECIf fstat=0Flush [MYT] : # Mandatory before CommitIf fstat=0CommitRETURN_MESSAGE="SUCCESS"EndEndifEndif# If fstat is not null, retry again the transaction without buffering to identify the first line in errorRollbackTrbegin [MYT][S]adxwrb=0Gosub CREATE_RECIf fstat=0 : # It might work at the second attempt# No Flush is necessary, because adxwrb is equal to 0RETURN_M_MESSAGE="SUCCESS"CommitElse: If not, the error message will contain the first line in errorRollbackEndifEnd# Buffered write loop$CREATE_RECFor I=1 To NUMBERSetinstance [F:MYT] With VALUES(I)Writeb [MYT]If fstat<>0 : RETURN_MESSAGE="ERROR ON LINE"-num$(I) : BreakNext IReturn
The Flush
instruction sends to the database the lines that have not already flushed when the Writeb syntax is used to create lines in the database. Note that an automatic flush is performed every [adxwrb]] lines.
When several lines have to be written in a database table, it is important to use the Writeb instruction instead of the Write syntax. The difference is that the lines are sent to the database by groups of adxwrb lines. This avoids too many exchanges with the database server and is therefore less time consuming. The only consequence is that the fstat value is returned only at flush time or for every adxwrb record. This means that the development partner can no longer identify the exact line where the error occurred; however, it is still possible to retry the create transaction with adxwrb equals to 0 (that disables the buffering).
It is mandatory to flush the write buffer before a Commit; otherwise, an error occurs if lines remain in the flush buffer at Commit.
Flush updates the fstat variable to give the result of the write operations, as indicated by the following table:
fstat value | Description |
---|---|
0 | The write operation has been successful. |
1 | The write operation failed due to locking issues. |
3 | The write operation failed on a duplicate key creation attempt. |
Trbegin, Commit, Rollback, Writeb, adxwrb, adxftl.