Copyright 2009, Oracle. All rights reserved. 11 - 2 Objectives After completing this lesson, you should be able to do the following: 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 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. 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 hrapp USING hr_context; Use the SET_CONTEXT procedure to: Create attributes Set values of attributes dbms_session.set_context ( 'hrapp', 'emp_id', v_emp_id ); 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 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. Copyright 2009, Oracle. All rights reserved. 11 - 7 Application Context Data Sources The built-in USERENV context contains session primitives as 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-entry attributes. Copyright 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 Copyright 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; Copyright 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_id FROM employees WHERE email = sys_context('userenv', 'session_user'); dbms_session.set_context ( 'hrapp', 'emp_id', v_emp_id ); Copyright 2009, Oracle. All rights reserved. 11 - 13 Step 3: Call the Package
Create a logon trigger that calls the HR_CONTEXT.SET_EMP_ID procedure: CREATE OR REPLACE TRIGGER hr_context_logon AFTER LOGON ON phall.SCHEMA BEGIN hr_context.set_emp_id(); END; / Copyright 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'); Copyright 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') Copyright 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 2 User Database Session 1 SGA Application context is EMP ID = 101 Copyright 2009, Oracle. All rights reserved. 11 - 18 How the Application Context Accessed Globally Works
9. Clears context PHALL Application server Database Copyright 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); Copyright 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. Copyright 2009, Oracle. All rights reserved. 11 - 24 Step 1: Create the Application Context Accessed Globally Create the context by using:
The ACCESSED GLOBALLY clause indicates that the context can be accessed from multiple sessions. CREATE CONTEXT hrapp USING hr_context ACCESSED GLOBALLY; Copyright 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); Copyright 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(); Copyright 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'); Copyright 2009, Oracle. All rights reserved. 11 - 28 Data Dictionary Views SQL> CREATE CONTEXT hrapp USING hr_context;
Context created.
SQL> SELECT * 2 FROM dba_context 3 WHERE namespace = 'HRAPP';
SQL> Copyright 2009, Oracle. All rights reserved. 11 - 30 Guidelines Attempting to change the context outside of its package results 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. Copyright 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 Copyright 2009, Oracle. All rights reserved. 11 - 33 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