KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Always make sure your Stored Procedure is able to gracefully quit
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: September 14, 2017
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

Stored Procedures should be written in such a way that they can gracefully stop when the database needs to shutdown.

For example, you may a Stored Procedure that checks a folder every 60 seconds and imports any data found, or maybe the stored procedure checks an email folder every 60 seconds and important the messages found into new records. You may want these to run indefinitely so you may have written them in such a way that allows them to run forever expecting 4D to abort them when a database shutdown is requested. Or maybe you didnt consider the database shutdown event at all when writing the Stored Procedure.

Whatever the case may be, the Stored Procedure must be written in such a way that it can gracefully stop and exit from the loop when a database shutdown is requested.

The following example provides a basic implementation for gracefully shutting down the Stored Procedure:

// Stored Procedure Example
C_BOOLEAN(boolFlagToCheck)
boolFlagToCheck:=False
REPEAT
  // do stuff
  DELAY PROCESS(CURRENT PROCESS;60*60) // delay 1 minute
UNTIL(boolFlagToCheck) | (Process aborted)


When the code above runs, the REPEAT loop will keep iterating Until the boolFlagToCheck variable is equal to True.

The following example can be used to gracefully exit from the loop:

C_LONGINT(StoredProcNum)
C_BOOLEAN(boolFlagToCheck)
SET PROCESS VARIABLE(StoredProcNum;boolFlagToCheck;True)
RESUME PROCESS(StoredProcNum)


When the code above runs it will set the boolFlagToCheck process variable to True. The new value will provide the condition for the REPEAT loop to complete, and the call to RESUME PROCESS will wake it up if it is delayed.

See Also: