1. Create a simple C function. (in this example I use basic factorial function)
[oracle@db /]$ cd /u01/app/oracle/product/ [oracle@db product]$ ls 11.2.0 [oracle@db product]$ mkdir c_lib [oracle@db product]$ ls 11.2.0 c_lib [oracle@db product]$ cd c_lib [oracle@db c_lib]$ touch factorial.c [oracle@db c_lib]$ cat factorial.c int getVal (int a){ if(a!=1){ return(a * getVal(a-1)); } else return 1; } [oracle@db c_lib]$1.1 Compile it and create a shared library.
[oracle@db c_lib]$ gcc -fPIC -c factorial.c [oracle@db c_lib]$ gcc -shared -o factorial.so factorial.o [oracle@db c_lib]$ ls factorial.c factorial.o factorial.so [oracle@db c_lib]$Now we have a shared library. Lets set up the listener.
2. Modify the listener.ora.
LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = db)(PORT = 1521)) (ADDRESS = (PROTOCOL = IPC)(KEY = extproc)) ) ) SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (SID_NAME = PLSExtProc) (ORACLE_HOME = /u01/app/oracle/product/11.2.0/dbhome_1) (PROGRAM = extproc) (ENVS = "EXTPROC_DLLS=/u01/app/oracle/product/c_lib/factorial.so") ) ) ADR_BASE_LISTENER = /u01/app/oracleRestart listener.
[oracle@db admin]$ lsnrctl stop listener LSNRCTL for Linux: Version 11.2.0.3.0 - Production on 05-FEB-2013 15:31:01 Copyright (c) 1991, 2011, Oracle. All rights reserved. Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db)(PORT=1521))) The command completed successfully [oracle@db admin]$ lsnrctl start listener LSNRCTL for Linux: Version 11.2.0.3.0 - Production on 05-FEB-2013 15:31:09 Copyright (c) 1991, 2011, Oracle. All rights reserved. Starting /u01/app/oracle/product/11.2.0/dbhome_1/bin/tnslsnr: please wait... TNSLSNR for Linux: Version 11.2.0.3.0 - Production System parameter file is /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora Log messages written to /u01/app/oracle/diag/tnslsnr/db/listener/alert/log.xml Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=db)(PORT=1521))) Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=extproc))) Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db)(PORT=1521))) STATUS of the LISTENER ------------------------ Alias listener Version TNSLSNR for Linux: Version 11.2.0.3.0 - Production Start Date 05-FEB-2013 15:31:09 Uptime 0 days 0 hr. 0 min. 0 sec Trace Level off Security ON: Local OS Authentication SNMP OFF Listener Parameter File /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora Listener Log File /u01/app/oracle/diag/tnslsnr/db/listener/alert/log.xml Listening Endpoints Summary... (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=db)(PORT=1521))) (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=extproc))) Services Summary... Service "PLSExtProc" has 1 instance(s). Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service... The command completed successfully [oracle@db admin]$Check the listener status:
[oracle@db admin]$ lsnrctl status listener LSNRCTL for Linux: Version 11.2.0.3.0 - Production on 05-FEB-2013 15:32:13 Copyright (c) 1991, 2011, Oracle. All rights reserved. Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db)(PORT=1521))) STATUS of the LISTENER ------------------------ Alias listener Version TNSLSNR for Linux: Version 11.2.0.3.0 - Production Start Date 05-FEB-2013 15:31:09 Uptime 0 days 0 hr. 1 min. 3 sec Trace Level off Security ON: Local OS Authentication SNMP OFF Listener Parameter File /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora Listener Log File /u01/app/oracle/diag/tnslsnr/db/listener/alert/log.xml Listening Endpoints Summary... (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=db)(PORT=1521))) (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=extproc))) Services Summary... Service "PLSExtProc" has 1 instance(s). Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service... Service "orcl" has 1 instance(s). Instance "orcl", status READY, has 1 handler(s) for this service... Service "orclXDB" has 1 instance(s). Instance "orcl", status READY, has 1 handler(s) for this service... The command completed successfully3. Create a library (as sysdba)
[oracle@db c_lib]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.3.0 Production on Mon Feb 4 18:01:02 2013 Copyright (c) 1982, 2011, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production With the Partitioning and Real Application Testing options SQL> create or replace library c_factorial as '/u01/app/oracle/product/c_lib/factorial.so' 2 / Library created. SQL> select owner, library_name, file_spec, status from all_libraries where lower(library_name) = 'c_factorial'; OWNER LIBRARY_NAME FILE_SPEC STATUS --------- --------------- ------------------------------------------ ------- SYS C_FACTORIAL /u01/app/oracle/product/c_lib/factorial.so VALID SQL> grant execute on c_factorial to oratt; Grant succeeded. SQL>4. Create a package (oratt user).
SQL> CREATE OR REPLACE PACKAGE c_pack AS 2 function factorial_func(x in integer) return pls_integer; 3 END; 4 / Package created. SQL> CREATE OR REPLACE PACKAGE BODY c_pack AS 2 function factorial_func(x in integer) 3 return pls_integer 4 as language c 5 library sys.c_factorial 6 name "getVal"; 7 END; 8 / Package Body created.5. Execute factorial_func function
SQL> set serveroutput on SQL> begin 2 dbms_output.put_line(c_pack.factorial_func(5)); 3 end; 4 / 120 PL/SQL procedure successfully completed. SQL> select c_pack.factorial_func(5) from dual; C_PACK.FACTORIAL_FUNC(5) ------------------------ 120 SQL>
Комментариев нет:
Отправить комментарий