Q1. "When is init_array called and lookup_array loaded into memory? When is the package compiled? When is it called for the first time? Is it called more than once?"
init_array is called when any functions or procedures in the package are called - i.e. "just in time". It will be called whenever the state of the packet is lost (i.e., it can be called more than once per session).
This has consequences for a scenario where the state of a package is lost - for example, when someone recompiles a package. In this case, the following sequence occurs:
First, the do_something call is called - init_array , then do_something is executed - your session now has some memory allocated in PGA to store the array.
My session recompiles the package. At this point, your session memory allocated for this package is marked as "invalid."
Your do_something session calls - Oracle detects that your session memory is marked as invalid and problems ORA-04061 "existing xxx state was invalid."
If your session calls do_something again, it runs without errors - it first calls init_array and then does do_something .
Q2. "Is there a better way to implement a static search array?"
I do not see any real problems with this approach as long as you take into account the behavior described above.
In some cases, I saw people launch an init call at the beginning of every function / procedure that needs an array - i.e. whenever do_something is called, it checks to see if it needs to initialize, and if it calls init_array . The advantage of this approach is that you can only configure init_array to initialize the bits that this function / procedure needs, which can be beneficial if init_array does a lot of work, which can help avoid a lot of one-time startup overhead for each session.
source share