Existing state of packages has been discarded

33,646

Solution 1

When a session makes use of a package that session retains some state of the package. If that package is recompiled the next time the same session references the package you'll get that error.

To avoid this make sure you disconnect each session that may have used the package or have the session do a DBMS_SESSION.RESET_PACKAGE to reset the package state.

Solution 2

If you recompile a package specification all dependant objects are invalidated. A dependant object is any view, package specification, package body, function or procedure that references any of the declarations in the recompiled package specification.

Also, as pointed out by darreljnz, sessions usually retain references to the state of packages they have accessed, causing an ORA-04068: existing state of packages has been discarded the next time the session tries to reference the package.

This latter behaviour is a real nuisence and makes it necessary to either write code to retry operations or to close all active sessions after installing a new version of a package (effectively restarting the application/service). Bottom line: It makes it harder to install hotfixes.

Solution 3

Use pragma serially_reusable in you Package and its Body.

Share:
33,646
Mr Man
Author by

Mr Man

Updated on July 09, 2022

Comments

  • Mr Man
    Mr Man almost 2 years

    So I have been running a PLSQL procedure just fine, and compiling with no errors. I made one change to my procedure, and it still compiles fine, but now when I run it, I get this error:

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

    Any ideas what this could be? The change I made was so insignificant that I doubt it could have caused this error. Thank you in advance for your help!

  • Yogesh Jindal
    Yogesh Jindal almost 12 years
    Thanks darreljnz, the solution worked fine for me. But at first I didn't understand what you were saing. So, I am just going to clarify your solution a little bit for others. Open a new test window in pl/sql and paste "Begin sys.dbms_session.reset_package; end;" and hit F9 to execute it and then make changes to your package or recompile the package and now we won't get any errors in our applications
  • Andrew Martinez
    Andrew Martinez almost 12 years
    Best start of an answer for this issue. It would be good to see this carried a bit further to explain the core issue and why the error occurs.
  • pauloya
    pauloya almost 10 years
    @YogeshJindal it looks like you are saying something different than darrelinjz. He wrote that you need to run the RESET_PACKAGE, on each session, after the package change to avoid the error.
  • Yogesh Jindal
    Yogesh Jindal almost 10 years
    @PauloManuelSantos I think he is saying either disconnect all sessions or run this command from a session to reset the package state.
  • pauloya
    pauloya almost 10 years
    @YogeshJindal What I read is that each session needs to run this command, which makes sense after reading what the command does, RESET_PACKAGE: "This procedure de-instantiates all packages in this session." It should not affect other sessions in the Database, so running it from a test window would not affect other sessions.
  • TecHunter
    TecHunter almost 7 years
    what does it do, why is this a solution? please elaborate
  • durette
    durette over 5 years
    Thank you for the tip, but it looks like this breaks triggers that depend on the package.