Sub topic 12
SERIALLY_REUSABLE Packages
This chapter assumes you know basic SQL, PL/SQL variables and control flow, standalone subprograms, and PL/SQL exception handling.
Why package state matters
A normal package can hold variables in its package state. Oracle creates that state when a session first references the package. The state remains available for later calls in the same database session.
That behavior is useful for session-level settings, caches, and counters. It can also retain more memory than necessary. In a connection pool, one physical database session may serve several logical users. A later user could observe state left by an earlier user if the application does not reset it.
PRAGMA SERIALLY_REUSABLE changes this lifecycle. It tells Oracle to keep the package state only for the duration of a server call. When that call ends, Oracle releases the state. The next call gets a fresh package instance.
A server call can be a client request that runs a PL/SQL block or invokes a stored procedure. Nested subprogram calls within that request use the same package instance.
Declaring a serially reusable package
Declare the pragma in the package specification and body when the package has both parts. The following package keeps a counter only during one server call:
CREATE OR REPLACE PACKAGE call_total AS
PRAGMA SERIALLY_REUSABLE;
PROCEDURE add_value(p_value IN PLS_INTEGER);
FUNCTION total RETURN PLS_INTEGER;
END call_total;
/
CREATE OR REPLACE PACKAGE BODY call_total AS
PRAGMA SERIALLY_REUSABLE;
g_total PLS_INTEGER := 0;
PROCEDURE add_value(p_value IN PLS_INTEGER) IS
BEGIN
g_total := g_total + p_value;
END add_value;
FUNCTION total RETURN PLS_INTEGER IS
BEGIN
RETURN g_total;
END total;
END call_total;
/The variable g_total is private because it is declared only in the body. Both add_value and total can use it during the same call.
Run this block as one server call:
BEGIN
call_total.add_value(2);
call_total.add_value(3);
DBMS_OUTPUT.PUT_LINE('First call: ' || call_total.total);
END;
/It prints First call: 5. The two additions and the function call share one package state.
Now run a separate block:
BEGIN
DBMS_OUTPUT.PUT_LINE('Second call: ' || call_total.total);
END;
/It prints Second call: 0. The first call ended, so Oracle discarded its state and initialized a new instance for the second call.
Regular versus serially reusable state
| Package kind | State lifetime | Typical concern |
|---|---|---|
| Normal package | Until session ends or state is reset | State can remain between logical users |
| Serially reusable package | Until the current server call ends | State cannot be used as a cross-call cache |
The pragma does not make data persistent. Store data in a table when it must survive calls, sessions, or database restarts. It also does not define a transaction boundary. COMMIT and ROLLBACK do not control the package-state lifetime.
Common errors and design choices
Do not use serially reusable state when a package is deliberately maintaining a session-wide value. For example, a session configuration package may need its settings during many separate calls.
Do use it for temporary working data, especially when the package processes large collections or buffers during one request. Releasing that memory at the end of the call can reduce session memory usage.
Remember that all subprograms called during one server call still share the same state. If a procedure invokes another procedure internally, the nested procedure does not receive a fresh package instance.
A serially reusable package is also not a replacement for proper cleanup. Close files, release locks, and remove temporary database objects using normal application logic. The pragma controls package variables and related package state.
Exercise
A package counter must retain its value for every call made by one database session. Should you add PRAGMA SERIALLY_REUSABLE?
Show answer
No. A serially reusable package discards its state at the end of each server call. Use a normal package or persistent table data, depending on how long the value must survive.