Sub topic 8
Package State and How Long It Lives
This chapter assumes you know basic SQL, PL/SQL variables and control flow, standalone stored procedures and functions, and basic exception handling.
A package can keep variables between calls. These variables are called package state. The state belongs to one database session, so later calls from the same session can see earlier changes.
Package state is not a table. Oracle stores it in memory associated with the session. It is not automatically shared with other sessions, and a transaction rollback does not restore its previous value.
A package with session state
The following package counts calls. The variables are declared in the package body, so callers cannot change them directly.
CREATE OR REPLACE PACKAGE call_counter AS
PROCEDURE hit;
FUNCTION hits RETURN PLS_INTEGER;
FUNCTION started_at RETURN TIMESTAMP WITH TIME ZONE;
END call_counter;
/
CREATE OR REPLACE PACKAGE BODY call_counter AS
g_hits PLS_INTEGER;
g_started TIMESTAMP WITH TIME ZONE;
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 WITH TIME ZONE IS
BEGIN
RETURN g_started;
END started_at;
BEGIN
g_hits := 0;
g_started := SYSTIMESTAMP;
END call_counter;
/
SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.PUT_LINE('Before hit: ' || call_counter.hits);
call_counter.hit;
call_counter.hit;
DBMS_OUTPUT.PUT_LINE('After two hits: ' || call_counter.hits);
DBMS_OUTPUT.PUT_LINE('Started: ' ||
TO_CHAR(call_counter.started_at, 'YYYY-MM-DD HH24:MI:SS'));
END;
/The package state is initialized the first time the session references call_counter. Both calls to hit use the same g_hits value. A typical result is:
Before hit: 0
After two hits: 2
Started: 2026-11-28 08:10:04The exact timestamp depends on the session clock.
The lifetime of package state
Package state normally follows this lifecycle:
A new session gets a new copy of every package variable. For example, if Session A calls call_counter.hit twice, Session A sees 2. Session B still sees 0 when it first references the package.
The state also disappears when the session disconnects. Reconnecting does not recover it. This makes package variables unsuitable for durable business data, counters that must survive restarts, or information that must be shared across users. Store such data in database tables instead.
Initialization and common errors
The initialization section is the executable block after the package body’s declarations and subprogram definitions. Oracle runs it once for a package instance, normally on the first reference in each session.
Initialization can fail. If it raises an exception, the first reference fails, and later references in that session can continue to fail until the session or package state is reset.
A common error is assuming that package state belongs to a human user rather than to a database session. Connection pools make this especially important. A pool may reuse one physical database session for several logical users. State left by one request can then affect another request.
Package state can also change after recompilation. If Oracle invalidates or replaces a package while a session is using it, the session may lose its existing state. Depending on the timing and object change, the next reference can raise an error such as ORA-04068, which reports that existing state of packages has been discarded. Code should not depend on package variables surviving deployment or recompilation.
Choosing package state
Use package state when the value is temporary, session-specific, and useful across several calls. Examples include a formatting preference, a per-session cache, or diagnostic information for the current unit of work.
Avoid it when:
- Multiple sessions must see the same value.
- The value must survive a disconnect.
- A connection pool can mix logical users on one session.
- The value must participate in transaction recovery.
- Recompilation must not affect the value.
A package with no variables is effectively stateless. Stateless packages are easier to test and safer in pooled applications.
Exercise
Session A calls call_counter.hit three times. Session B then references call_counter.hits for the first time. What value should Session B receive, and why?
Show answer
Session B receives 0. Each database session gets a separate package instance, so Session B cannot see Session A’s package state.