Tuesday, April 14, 2015

PL/SQL- Package Data Session persistence, how to use it, using PRAGMA SERIALLY_REUSABLE

In this post I will try to show the session's persistence of Package  Data, what are the issues with the Default behaviour and what can be done to alter this behaviour.

Session: When ever a Client connects from a machine to a Database server, a session is set up between the server and the Client, now this session is responsible for any communication between the server and the client.
If the same user logs in  from different client/machine then a separate session will be created.

In Oracle the data for a particular session resides in the PGA.

What is meant by Package Data persistence for a Session?
 The Default behaviour for a Package Data is once the variables in the Packages are assigned then they will hold their values until the session end or Disconnects.

Lets look at examples to understand the issue better.


Note: All code mentioned here can be executed in the HR schema provided with Oracle installation 11g.

Create a new session using Sql/Developer

CREATE OR REPLACE PACKAGE DEMO1
AS
employee_name VARCHAR2(256);
PROCEDURE EMPLOYEE_SALARY(e_name IN VARCHAR2);
END DEMO1;

CREATE OR REPLACE PACKAGE BODY DEMO1
AS
PROCEDURE EMPLOYEE_SALARY(e_name IN VARCHAR2)
AS
BEGIN
SELECT last_name into employee_name FROM EMPLOYEES WHERE first_name=e_name;
END;
END;

This is a simple package which is going to give the last name of the employee when the first name of the employee is used a Parameter in the EMPLOYEE_SALARY procedure.

Execute the below block now:

SET SERVEROUTPUT ON ;
BEGIN
DEMO1.EMPLOYEE_SALARY('Neena');
dbms_output.put_line('The name of the employee is '||DEMO1.employee_name);
END;

output of the block is :
anonymous block completed
The name of the employee is Kochhar

Now the DEMO1.employee_name variable is assigned this value and until this value is over written with another call with a different parameter value, the value will persist for the session.

So if you only execute the below block:

SET SERVEROUTPUT ON ;
BEGIN
dbms_output.put_line('The name of the employee is '||DEMO1.employee_name);
END;

Still the output will be :
anonymous block completed
The name of the employee is Kochhar

Now execute the below block from a different session, so for this open the SQL PLUS :

You will see that for this session, employee_name variable is not yet assigned any value as this is a new session.

This data persistence at session level is useful, but it can easily cause memory issues as the session variables will remain in the PGA for the whole session and eat up the memory area and will decrease the performance.

In order to overcome this behaviour we can make Oracle Package data to persist only for a specific call within the session using a PRAGMA SERIALLY_REUSABLE.

Lets look at one example for this :

CREATE OR REPLACE PACKAGE DEMO2
AS
PRAGMA SERIALLY_REUSABLE;
employee_name VARCHAR2(256);
PROCEDURE EMPLOYEE_SALARY(e_name IN VARCHAR2);
END DEMO2;

CREATE OR REPLACE PACKAGE BODY DEMO2
AS
PRAGMA SERIALLY_REUSABLE;
PROCEDURE EMPLOYEE_SALARY(e_name IN VARCHAR2)
AS
BEGIN
SELECT last_name into employee_name FROM EMPLOYEES WHERE first_name=e_name;
END;
END;


SET SERVEROUTPUT ON ;
BEGIN
DEMO2.EMPLOYEE_SALARY('Nancy');
dbms_output.put_line('The name of the employee is '||DEMO2.employee_name);
END;

Output for this is 
anonymous block completed
The name of the employee is Greenberg

Execute the second block:

SET SERVEROUTPUT ON ;
BEGIN
dbms_output.put_line('The name of the employee is '||DEMO2.employee_name);
END;

Output is :
anonymous block completed
The name of the employee is 

As compared to the previous case now the data persist only for a specific call and not for the whole session!!!!!

Hope this helps............

Wednesday, April 8, 2015

ODI- Static Error Control and Data Quality checks using CKM in Oracle Data Integrator

In order to perform Data Quality checks and initial cleaning of the data ODI provides 2 kinds of DQ checks: Static Control and Flow Control.
Static Control->This is used to check the quality of the data that is already present in the Data store, that is why this is known as Static.
Flow Control->This is used to check the quality of the data at the time of Integration process, that is when the data is being processed into the Target table, at that time we can separate out the error records (i.e. the records that do not full fill the constraints set for the Data Quality)

In this post we will see Static control:

Take the SCOTT schema and the EMP table from the schema.

Now add one record to the table:

INSERT INTO SCOTT.EMP
VALUES (1234,'Test','Test',null,sysdate,1000,NULL,10);


Assume that this record came while extract process and this is bad data and now you want to do a static check of your data and want to know quality before initiating the transfers to Target database .

ODI part:
1. Reverse Engg this table to some Model in ODI.

2. Import your technology specific CKM into the Project as shown in the screen shot.


3. In the Emp datastore add one constraint.

4. Start the DQ check process.
5. Once the session is complete Click on Control-Errors, this will display the records that do not satisfy the constraint.

Understanding the process (CKM)
1. The operator window for your session will look something like this.


2. One SNP_CHECK_TAB is created in the work schema of the Default physical schema.
3. E$ tables will be created in the work Schema.
4. The insert CK errors step will display the code like this:
from SCOTT.EMP  EMP
where not (
ENAME<>'Test'
)

this is where the data is getting filtered out into the E$ table.