An Application Context example

Creating a Context

Context is a set of session-based attributes that can be only set by the package specified in CREATE CONTEXT statement.

   CREATE CONTEXT app1_ctx USING app1_ctx_package;

In this example, only procedures and functions of app1_ctx_package package are allowed to set or reset the attributes of the app1_ctx context.

Setting Attributes of Context

Only the package specified in CREATE CONTEXT statement can change attributes of the context using DBMS_SESSION.SET_CONTEXT procedure:

    CREATE OR REPLACE PACKAGE app1_ctx_package IS
PROCEDURE set_empno (empno NUMBER);
END;
/
CREATE OR REPLACE PACKAGE BODY app1_ctx_package IS
PROCEDURE set_empno (empno NUMBER) IS
BEGIN
DBMS_SESSION.SET_CONTEXT('app1_ctx', 'empno', empno);
END;
END;
/

Note. You have to create the package (or just its specification) before you create the context.

An application can initialize context by calling the package procedure i.e

   CALL app1_ctx_package.set_empno(11);

Restricting Data Access using Context

Assume, you have the following data:

   CREATE TABLE orders (price NUMBER(10,2), empno NUMBER(5));
 
INSERT INTO orders VALUES (295.00, 10);
INSERT INTO orders VALUES (99.00, 11);
INSERT INTO orders VALUES (125.00, 11);

Then you can restrict access to this table, so an employee can see only own data using SYS_CONTEXT function:

   SELECT * FROM orders WHERE empno = SYS_CONTEXT('app1_ctx', 'empno');

Result:

price empno
99 11
125 11

This example shows how application can restrict access to data by adding WHERE condition with SYS_CONTEXT function.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7734298/viewspace-709479/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7734298/viewspace-709479/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值