Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Sai Pradyumn.

Asked: December 09, 2016 - 6:12 pm UTC

Last updated: December 12, 2016 - 1:11 am UTC

Version: 11

Viewed 1000+ times

You Asked

Hi

I had defined the few global variables in the Package Specification.One of the main procedure is initializing some values into those variables.
In the main procedure we are calling different sub procedures. But unfortunately some times global variables are becoming null & Some times i am able to get the value which I initialized in my main procedure .

Could you please let me know functionality of the variable which is defined in spec & initialized in the one procedure.

and Connor said...

Packages are initialized on first invocation in the session. So if you

- start a new session, or
- recreate the package in some way

then you will lose the existing state of those variables

SQL> create or replace
  2  package PKG is
  3    v int;
  4  end;
  5  /

Package created.

SQL> set serverout on
SQL> exec pkg.v := 10;

PL/SQL procedure successfully completed.

SQL> exec dbms_output.put_line('v='||pkg.v);
v=10

PL/SQL procedure successfully completed.

SQL> conn scott/tiger
Connected.

SQL> set serverout on
SQL> exec dbms_output.put_line('v='||pkg.v);
v=

PL/SQL procedure successfully completed.

SQL> exec pkg.v := 12;

PL/SQL procedure successfully completed.

SQL> create or replace
  2  package PKG is
  3    v1 int;
  4    v int;
  5  end;
  6  /

Package created.

SQL> exec dbms_output.put_line('v='||pkg.v);
v=




Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Excellent explanation

Sai Pradyumn, December 11, 2016 - 5:29 pm UTC

Hi McDonald. Thank you very much for valuable explanation.Its really very help for me. Is this kind of behaviour is applicable only for global variable in the package apec. What about the public variables which are declared in package body directly (not part of any procedure) I am calling these packages through the Shell script which invokes a java appllication. From this java application we are calling tha data base procedure. If I am calling same packages with different parameters sequentially ,second execution will create a new session ? Or it will continue with existing session only
Connor McDonald
December 12, 2016 - 1:11 am UTC

Package *body* global variables work in the same way, except that the only thing that can reference them is code within the same package body.

Assuming your java application makes a single connection to the database, then you would be ok. But many applications like this make use of a connection pool facility, ie, several sessions share the load. If this is the case, then multiple calls might use different sessions and hence you would not be able to rely on the value of the variable across calls.

Hope this helps.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library