由于项目为了缩短开发时间,要在Oracle中调用外部已经开发好的C程序。
查找了网上的一些帖子,再结合实际总结这篇文章。希望对大家有所帮助。
参考文章:http://www.cnblogs.com/mathitlin99/archive/2013/09/05/3303717.html

首先编译外部动态库libDialPlan.so

点击(此处)折叠或打开

  1. gcc -fPIC -c LibDialPlan.cpp
  2. gcc -shared -o libDialPlan.so LibDialPlan.o -lstdc++

修改Oracle TNS文件和监听文件。
不清楚大家直接拷贝是否可用。如果有问题,请注意一下等号以及括号两边的空格
tnsnames.ora

点击(此处)折叠或打开

  1. ORADB =
  2.   (DESCRIPTION =
  3.     (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.69.166)(PORT = 1521))
  4.     (CONNECT_DATA =
  5.       (SERVER = DEDICATED)
  6.       (SERVICE_NAME = oradb)
  7.     )
  8.   )

  9. EXTPROC_CONNECTION_DATA=
  10.   (DESCRIPTION=
  11.     (ADDRESS_LIST=
  12.     (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
  13.   )
  14.   (CONNECT_DATA=
  15.     (SID = PLSExtProc)
  16.     (PRESENTATION = RO)
  17.   )
  18.   )
listener.ora

点击(此处)折叠或打开

  1. LISTENER =
  2.   (DESCRIPTION_LIST =
  3.     (DESCRIPTION =
  4.       (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
  5.       (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.69.166)(PORT = 1521))
  6.     )
  7.   )

  8. ADR_BASE_LISTENER = /home/oracle/db/app


  9. SID_LIST_LISTENER =
  10. (SID_LIST =
  11.   (SID_DESC =
  12.   (GLOBAL_DBNAME = oradb)
  13.   (SID_NAME = oradb)
  14.   )
  15.   (SID_DESC=
  16.   (SID_NAME = PLSExtProc)
  17.   (ORACLE_HOME = /opt/local/oracle/app/product/11.2.0/dbhome_2)
  18.   (ENVS = EXTPROC_DLLS=ANY)
  19.   (PROGRAM = extproc)
  20.   )
  21. )

修改后重启监听:lsnrctl stop ;lsnrctl start
查看监听状态,应该出现如下的监听项:PLSExtProc 

点击(此处)折叠或打开

  1. [root@localhost admin]# lsnrctl status

  2. LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 04-AUG-2016 19:33:50

  3. Copyright (c) 1991, 2009, Oracle. All rights reserved.

  4. Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
  5. STATUS of the LISTENER
  6. ------------------------
  7. Alias LISTENER
  8. Version TNSLSNR for Linux: Version 11.2.0.1.0 - Production
  9. Start Date 04-AUG-2016 16:23:54
  10. Uptime 0 days 3 hr. 9 min. 55 sec
  11. Trace Level off
  12. Security ON: Local OS Authentication
  13. SNMP OFF
  14. Listener Parameter File /opt/local/oracle/app/product/11.2.0/dbhome_2/network/admin/listener.ora
  15. Listener Log File /opt/local/oracle/app/product/11.2.0/dbhome_2/network/log/listener.log
  16. Listening Endpoints Summary...
  17.   (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
  18.   (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.69.166)(PORT=1521)))
  19. Services Summary...
  20. Service "PLSExtProc" has 1 instance(s).
  21.   Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
  22. Service "oradb" has 1 instance(s).
  23.   Instance "oradb", status UNKNOWN, has 1 handler(s) for this service...
  24. The command completed successfully
  25. [root@localhost admin]#
创建Oracle  library 对象

点击(此处)折叠或打开

  1. create or replace library hytpdtbilldb.Fun_Ext_Proc
  2.   as '/opt/local/Oracle_install/Oracle_Call_External_C/libDialPlan.so';
创建函数
1)按照网上的文章,还有一个parameter的参数。
但是我加入这个参数后一直不能编译通过。去掉后并未影响函数的使用。
2)数字和字符串对应的是binary_integer,varchar2类型。本来C的入参是需要数组类型,但是Oracle没有真正的数组,只能通过字符串代替。
3)在我测试的过程中,偶尔需要重启才能正常调用函数。大家使用的过程中可以留意一下。

点击(此处)折叠或打开

  1. create or replace function hytpdtbilldb.Air2Gsi
  2. (x binary_integer,y varchar2)
  3. return varchar2
  4. as
  5. language C
  6. library hytpdtbilldb.Fun_Ext_Proc
  7. name "Air2Gsi";
  8. /


09-05 17:11