Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/SQL Package invalidated

Tags:

oracle

plsql

I have a script that makes use of a package (PKG_MY_PACKAGE). I will change some of the fields in a query in that package and then recompile it (I don't change or compile any other packages). I run the script and I get an error that looks like

     ORA-04068: existing state of packages has been discarded     ORA-04061: existing state of package body "USER3.PKG_MY_PACKAGE" has been invalidated     ORA-04065: not executed, altered or dropped package body "USER3.PKG_MY_PACKAGE"     ORA-06508: PL/SQL: could not find program unit being called: "USER3.PKG_MY_PACKAGE"     ORA-06512: at line 34 

I run the script again (without changing anything else in the system) and the script executes successfully.

I thought that when I compiled before I executed the script that would fix any invalid references. This is 100% reproducible, and the more I use this script the more annoying it gets. What could cause this, and what would fix it?

(oracle 10g, using PL/SQL Developer 7)

like image 586
FrustratedWithFormsDesigner Avatar asked Mar 23 '10 18:03

FrustratedWithFormsDesigner


People also ask

What causes Oracle packages to become invalid?

Packages become invalidated when any database object (table, view etc.) that that package has dependecies on is altered in any way. When a Package is being used it is loaded into memory and a "session state" is created for that package.

When a procedure package is getting invalidated?

A package may become invalid for various reasons such as in the following cases: Table Invalid: One or more objects referred by the procedure or package has been altered the procedure wil be marked as invalid. For example, this can happen when one column has been added in a table referred to inside the package.

How do you check the package is valid or not in Oracle?

SQL> SQL> select object_type, status 2 from user_objects 3 where object_name = 'PKG'; OBJECT_TYPE STATUS ----------------------- ------- PACKAGE VALID PACKAGE BODY VALID 2 rows selected. You can see that DDL on EMP (which PKG depends on) made it invalid.

How do you resolve an invalid object in Oracle?

Oracle recommends that you run the utlrp.sql script after you install, patch, or upgrade a database, to identify and recompile invalid objects. The utlrp.sql script recompiles all invalid objects. Run the script immediately after installation, to ensure that users do not encounter invalid objects.


1 Answers

Background

existing state of packages has been discarded means, that your Package had some sort of state.

This is caused by a global variable stored in your Package Body.
Until 11.2.0.2, constants did also cause this behavior (see documentation).

Since the package has already been used in your session, Oracle assumes that this state is relevant for you. Some of these variables might have different values now, and when you recompile the Body, the values are reset.

This exception is thrown, so that your clients know that they can't rely on those variables any more.

Solutions

  • Remove all global variables and constants (before 11gR2) from the Package Body if possible
  • Replace global variables by DETERMINISTIC functions (as suggested by this answer)
  • Defining packages with PRAGMA SERIALLY_REUSABLE causes Oracle to re-initialize the global variables with every call to the server.
  • Close your session and reconnect before calling the package again.
  • Reset the state manually (see Paul James' answer)
like image 86
Peter Lang Avatar answered Sep 22 '22 13:09

Peter Lang