Sub topic 9
The Initialization Section
Assume you know basic SQL, PL/SQL variables and control flow, standalone subprograms, and basic exception handling. This chapter focuses on how a package prepares its session-specific state.
What the initialization section does
A package body can end with an initialization section. This section is an executable block that runs when Oracle first instantiates the package in a database session.
A package is instantiated when the session first references it. The reference can call a procedure, call a function, read a public variable, or use another public item. The initialization section runs before that reference continues.
A normal package is instantiated once per session. Its package-level variables then keep their values for the rest of that session.
Syntax and execution order
The initialization section follows all declarations and subprogram definitions in the package body. It starts with BEGIN and ends with the package body's final END statement.
CREATE OR REPLACE PACKAGE call_counter AS
PROCEDURE hit;
FUNCTION hits RETURN PLS_INTEGER;
FUNCTION started_at RETURN TIMESTAMP;
END call_counter;
/
CREATE OR REPLACE PACKAGE BODY call_counter AS
g_hits PLS_INTEGER;
g_started TIMESTAMP;
PROCEDURE hit IS
BEGIN
g_hits := g_hits + 1;
END hit;
FUNCTION hits RETURN PLS_INTEGER IS
BEGIN
RETURN g_hits;
END hits;
FUNCTION started_at RETURN TIMESTAMP IS
BEGIN
RETURN g_started;
END started_at;
BEGIN
-- This section runs once when the package is first used in a session.
g_hits := 0;
g_started := SYSTIMESTAMP;
END call_counter;
/The declarations create package-level variables. The subprograms can read and change those variables. The final BEGIN block assigns their starting values.
The following block uses the package twice. The initialization section runs before the first call to hit. It does not reset g_hits before the second call.
BEGIN
call_counter.hit;
DBMS_OUTPUT.PUT_LINE('Hits: ' || call_counter.hits);
call_counter.hit;
DBMS_OUTPUT.PUT_LINE('Hits: ' || call_counter.hits);
DBMS_OUTPUT.PUT_LINE(
'Started: ' || TO_CHAR(call_counter.started_at, 'YYYY-MM-DD HH24:MI:SS')
);
END;
/A typical result is:
| Call sequence | Value of `g_hits` |
|---|---|
| After the first `hit` call | 1 |
| After the second `hit` call | 2 |
Each new database session receives a separate package instance. A connection pool can therefore produce surprising results if an application expects package state to belong to one web request. The state belongs to the database session, not necessarily to the end user.
Initialization failures
Initialization code can perform setup work, but it can also fail. If an exception escapes from the initialization section, the package is not successfully instantiated and the exception reaches the caller.
Keep initialization short and predictable. Avoid unnecessary queries, network calls, or operations that can fail because of temporary conditions. If the package is later referenced again, Oracle may attempt initialization again because the earlier instantiation did not complete.
Recompiling a package can also affect existing package state. Sessions that already hold an instance may lose that state when Oracle invalidates the instance. Code should not treat package variables as durable storage.
Initialization and `SERIALLY_REUSABLE`
A package marked with PRAGMA SERIALLY_REUSABLE has shorter-lived state. Oracle can release that state at the end of a server call and create it again for a later call. Its initialization section can therefore run more often than once per session.
Use this feature only when you understand the call boundaries and do not need state to persist across calls. For ordinary package state, the default behavior is session duration.
Design practices
- Initialize every package variable to a clear, valid value.
- Keep setup logic small and deterministic.
- Put reusable setup procedures in the body, but call them from the initialization section when that improves clarity.
- Do not store durable business data only in package variables.
- Document whether callers may depend on state persisting between calls.
- Test the package with more than one database session.
Exercise
A session calls call_counter.hit three times. How many times does the initialization section run, and what value does call_counter.hits return?
Show answer
The initialization section runs once, before the first call. The function returns 3 after the third call.