using application context

25
Copyright © 2009, Oracle. All rights reserved. Using Application Contexts

Upload: arif-rakhman

Post on 03-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 1/25

Copyright © 2009, Oracle. All rights reserved.

Using Application Contexts

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 2/25

Copyright © 2009, Oracle. All rights reserved.11 - 2

Objectives

 After completing this lesson, you should be able to do thefollowing:

• Describe how an application context is used

• Describe the sources of application context values

• Implement a local context• Implement an application context that is accessed globally

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 3/25

Copyright © 2009, Oracle. All rights reserved.11 - 3

Application Context: Description

 An application context is a memory container with attributes:• The container is called a namespace.

•  A namespace has attributes.

• Each namespace is independent of others.

• The namespace is populated by a package.

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 4/25

Copyright © 2009, Oracle. All rights reserved.11 - 4

Namespace

Use the CREATE CONTEXT command to:

• Create a context in a namespace

•  Associate a package with the context

HRAPP

CREATE CONTEXT hrappUSING hr_context;

Use the SET_CONTEXT procedure to:

• Create attributes 

• Set values of attributes

dbms_session.set_context ('hrapp', 'emp_id', v_emp_id );

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 5/25

Copyright © 2009, Oracle. All rights reserved.11 - 5

Using the Application Context

 An application context:• Is read by applications

• Can be used to:

 –  Authorize users

 – Limit access to data, called by a FGAC policy – Set attributes used in the application

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 6/25

Copyright © 2009, Oracle. All rights reserved.11 - 6

Setting the Application Context

• The context attributes are set by a package, which: – Creates attributes in the context

 –  Assigns values to the attributes of the context

 – Is usually called when a user connects

Each application can use one or more contexts.•  A context may be used by multiple applications.

•   USERENV is a built-in context with session properties and is

available to all applications.

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 7/25Copyright © 2009, Oracle. All rights reserved.11 - 7

Application Context Data Sources

• The built-in USERENV context contains session primitivesas attributes.

 – Example: Client IP address

•  A local context uses database objects. The developer sets

these attributes. – Example: The EMPLOYEE_ID column in the EMPLOYEES 

table

•  An externalized context can get values from an external

source, such as Oracle Call Interface (OCI).

•  A global context uses values from the directory-entryattributes.

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 8/25Copyright © 2009, Oracle. All rights reserved.11 - 9

Implementing a Local Context

1. Create an application context.2. Create a PL/SQL package that sets the context.

3. Call the package to set the context attribute.

4. Read the context attribute in the application.

Application

context

PL/SQL package PL/SQL program

Set Read

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 9/25Copyright © 2009, Oracle. All rights reserved.11 - 10

Step 1: Create an Application Context

• Create a unique context:

 – Names the context HRAPP

 –  Associates it with an HR_CONTEXT package

• You can set the context attributes only in the: – Package named in CREATE CONTEXT

 – Function associated with a policy

• In the package, set attributes by calling

DBMS_SESSION.SET_CONTEXT.

•  Alternatively, you can use Enterprise Manager.

CREATE CONTEXT hrapp USING hr_context;

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 10/25Copyright © 2009, Oracle. All rights reserved.11 - 12

Step 2: Create a PL/SQL Package

That Sets the Context

Create the HR_CONTEXT.SET_EMP_ID procedure:• Use SYS_CONTEXT to get the session username:

• Use the session username to get the employee ID:

• Use SET_CONTEXT to set a context attribute:

sys_context('userenv', 'session_user');

SELECT employee_id INTO v_emp_idFROM employees WHERE email =

sys_context('userenv', 'session_user');

dbms_session.set_context ('hrapp', 'emp_id', v_emp_id );

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 11/25Copyright © 2009, Oracle. All rights reserved.11 - 13

Step 3: Call the Package 

Create a logon trigger that calls theHR_CONTEXT.SET_EMP_ID procedure:

CREATE OR REPLACE TRIGGER hr_context_logon AFTER LOGON ON phall.SCHEMA

BEGINhr_context.set_emp_id();END;/

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 12/25Copyright © 2009, Oracle. All rights reserved.11 - 14

Step 4: Read the Context Attribute

in the Application

• To return an attribute value, use:

• There are two arguments:

 – Name of the context

 –Name of the attribute

• Example in SELECT:

sys_context('hrapp', 'emp_id')

SELECT * FROM departments WHERE manager_id =

sys_context('hrapp','emp_id');

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 13/25Copyright © 2009, Oracle. All rights reserved.11 - 15

SYS_CONTEXT PL/SQL Function

•   SYS_CONTEXT returns context attributes:sys_context ('context','attribute') 

• To return the client IP address from the built-in context,

use:

• To return EMP_ID from the HRAPP context, use:

sys_context ('userenv', 'ip_address')

sys_context ('hrapp', 'emp_id')

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 14/25Copyright © 2009, Oracle. All rights reserved.11 - 16

Application Context Accessed Globally

• Shares a context across sessions• Simplifies connection pooling from a middle tier

• Uses a client identifier to identify the user of a session

PL/SQL program A PL/SQL program B

User Database Session 2User Database Session 1

SGA

Application context is EMP ID = 101

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 15/25Copyright © 2009, Oracle. All rights reserved.11 - 18

How the Application Context Accessed Globally

Works

2. Logs in

6. Makes another request

8. Logs out

1. Builds connection pool

3. Establishes session

4. Processes request

5. Completes request

7. Processes second request

9. Clears context

PHALL Application server Database 

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 16/25Copyright © 2009, Oracle. All rights reserved.11 - 20

PL/SQL Packages and Procedures

DBMS_SESSION manages:• Contexts:

• Global identifiers:

dbms_session.set_context('hrapp', 'emp_id', v_emp_id );

dbms_session.set_identifier(12345);

set_context(context, attribute, value );

dbms_session.set_identifier(client_id);

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 17/25Copyright © 2009, Oracle. All rights reserved.11 - 23

Implementing the Application Context Accessed

Globally

1. Create the application context accessed globally.2. Modify the program that establishes a session:

 – Set the application context.

 – Set the session client identifier.

 –

Clear the client identifier when the request ends.3. Modify the application program that handles subsequent

requests in the same session:

 – Set the session client identifier from this session.

 – Clear the client identifier when the request ends.

4. Create or modify the application program that ends a

session to clear the context.

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 18/25

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 19/25Copyright © 2009, Oracle. All rights reserved.11 - 25

Step 2: Establish a Session

1. Get a unique value to use as a client identifier.2. Set the application context:

3. Set the session client identifier:

4. Save the client identifier in a cookie.

dbms_session.set_context('hrapp','id','phall','APPSMGR', 12345 );

dbms_session.set_context('hrapp','dept','sales','APPSMGR', 12345 );

dbms_session.set_identifier( 12345 );

dbms_session.set_context(context, attr, value, username, client_id);

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 20/25Copyright © 2009, Oracle. All rights reserved.11 - 26

Step 3: Handle Subsequent Requests

1. Retrieve the client identifier.2. Set the client identifier for this session:

3. Clear the client identifier when the request ends:

dbms_session.set_identifier( 12345 );

dbms_session.clear_identifier();

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 21/25Copyright © 2009, Oracle. All rights reserved.11 - 27

Step 4: End a Session

1. Retrieve the client identifier.2. Clear the context:

EXEC dbms_session.clear_context('HRAPP', '12345');

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 22/25Copyright © 2009, Oracle. All rights reserved.11 - 28

Data Dictionary Views

SQL> CREATE CONTEXT hrapp USING hr_context;

Context created.

SQL> SELECT *2 FROM dba_context3 WHERE namespace = 'HRAPP';

 NAMESPACE SCHEMA PACKAGE TYPE

--------- ------ ---------- ----------------HRAPP SYS HR_CONTEXT ACCESSED LOCALLY

SQL>

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 23/25Copyright © 2009, Oracle. All rights reserved.11 - 30

Guidelines

•  Attempting to change the context outside of its packageresults in the following error message:ORA-01031: insufficient privileges. 

•   SYS_CONTEXT works much like a bind variable.

Versioning does not apply to contexts accessed globally.• There are parallel query and Real Application Clusters

(RAC) limitations.

• Context sources must be validated.

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 24/25Copyright © 2009, Oracle. All rights reserved.11 - 32

Practice 11 Overview:

Creating an Application Context

This practice covers the following topics:• Creating an application context

• Setting the context with a secure package

• Testing the application context

8/11/2019 Using Application Context

http://slidepdf.com/reader/full/using-application-context 25/25

Summary

In this lesson, you should have learned how to:• Use an application context

•  Access the sources of application context values

• Implement a local context

• Implement an application context that is accessed globally