Use a persistent procedure as shared code
- Last Updated: August 2, 2023
- 2 minute read
- OpenEdge
- Version 13.0
- Documentation
Because multiple procedures can access the handle of a persistent procedure, they can all share that single instance of the procedure, if appropriate. This feature lets you implement procedures that act as libraries of shared routines that many other procedures need. In this case, you need to make sure that the code in the procedure is truly shareable. This means that routines in the procedure should not save any context information that is needed between calls, because there is often no way of predicting whether another procedure might run the same routines at that time.
For example, suppose one internal procedure in the shared persistent procedure instance saves off a value in a variable defined in the Definitions section of the procedure. The variable identifies the calling procedure in some way, storing an attribute that is used in the program logic. Because the variable is scoped to the whole procedure file, its value is persistent between calls to various internal procedures and is accessible to all of them. Now suppose that some other procedure runs the same routine in the persistent procedure, resetting the variable value. Then the original caller runs some second routine in the persistent procedure that needs that value. The value is then wrong, because the other requesting procedure placed its own request in between calls.
The general rule to avoid this kind of problem is: do not store any persistent data in a procedure that is going to be shared in this way. Use only variables and other values defined within the internal procedure that uses them, because these values go out of scope and are reset when the internal procedure call ends.