oci_pconnect()
(PHP 5, PHP 7, PECL OCI8 >= 1.1.0)
使用一个持久连接连到 Oracle 数据库
说明
oci_pconnect(string $username,string $password[,string $db[,string $charset[,int $session_mode]]]): resource
oci_pconnect()创建一个到 Oracle 服务器的持久连接并登录。持久连接会被缓冲并在请求之间重复使用,可以降低每个页面加载的消耗。一个典型的 PHP 应用程序对于每个 Apache 子进程(或者 PHP FastCGI/CGI 进程)会有一个打开的持久连接到 Oracle 服务器。更多信息见数据库持久连接一节。
Note:自 OCI8 扩展库版本 1.1 起,持久 Oracle 连接的生命周期和最大数目可以通过设定以下配置选项来调整:oci8.persistent_timeout,oci8.ping_interval和oci8.max_persistent。
可选的第三个参数可以是本地 Oracle 实例的名字或者是在tnsnames.ora的条目中的名字。如果没有指定第三个参数, PHP 使用环境变量ORACLE_SID和TWO_TASK来分别确定本地 Oracle 实例的名字和tnsnames.ora的位置。
$session_mode参数自版本 1.1 起可用并接受如下值:OCI_DEFAULT
,OCI_SYSOPER
和OCI_SYSDBA
。如果指定了OCI_SYSOPER
或OCI_SYSDBA
其中之一,oci_new_connect()将尝试使用外部认证信息建立特权连接。特权连接默认被禁止。要启用,需要将oci8.privileged_connect设为 On。
使用 Oracle客户端库来确定字符集。字符集不需要与数据库的字符集相匹配。如果不匹配,Oracle会尽可能地将数据从数据库字符集进行转换。因为依赖于字符集,可能不能给出可用的结果。转换也增加一些时间开销。
如果不指定,Oracle 客户端用NLS_LANG
环境变量来决定字符集。
传递此参数可减少连接时间。
oci_pconnect()返回连接标识符,出错则返回FALSE
。
Note:
在 PHP 5.0.0 之前的版本必须使用ociplogon()替代本函数。该函数名仍然可用,为向下兼容作为oci_pconnect()的别名。不过其已被废弃,不推荐使用。
参见oci_connect()和oci_new_connect()。
参数
- $username
The Oracle user name.
- $password
The password for$username.
- $connection_string
包含要连接的Oracle 实例。可以是» Easy Connect 串,或是tnsnames.ora文件中的连接名,或是本地 Oracle 实例名。
如果不指定,PHP 使用环境变量来确定连接的Oracle 实例,诸如
TWO_TASK
(Linux 下)或LOCAL
(Windows 下)与ORACLE_SID
等。要使用 Easy Connect 命名方法,PHP 必须与 Oracle 10g 或更高版本的客户端库进行链接。Oracle 10g 的 Easy Connect 串格式:[//]host_name[:port][/service_name]。Oracle 11g 则为:[//]host_name[:port][/service_name][:server_type][/instance_name]。服务名可在数据库服务器机器上运行 Oracle 实用程序lsnrctl status找到。
tnsnames.ora文件可在 Oracle Net 查找路径中,此路径包括$ORACLE_HOME/network/admin和/etc。另一种方法是设置TNS_ADMIN以便通过$TNS_ADMIN/tnsnames.ora来读取。表确认 web 守护进程可读取此文件。
- $character_set
使用 Oracle客户端库来确定字符集。字符集不需要与数据库的字符集相匹配。如果不匹配,Oracle会尽可能地将数据从数据库字符集进行转换。因为依赖于字符集,可能不能给出可用的结果。转换也增加一些时间开销。
如果不指定,Oracle 客户端用
NLS_LANG
环境变量来决定字符集。传递此参数可减少连接时间。
- $session_mode
此参数在 PHP 5(PECL OCI81.1)版本开始可用,并收受下列值:
OCI_DEFAULT
,OCI_SYSOPER
和OCI_SYSDBA
。如为OCI_SYSOPER
或OCI_SYSDBA
其中之一,此函数将会使用外部的证书建立有特权的连接。有特权的连接默认是禁用的。需要将oci8.privileged_connect设为On来启用。PHP 5.3(PECL OCI8 1.3.4)引进了
OCI_CRED_EXT
模式值。使用外部或操作系统认证必需在 Oracle 数据库中进行配置。OCI_CRED_EXT
标志只可用于用户为"/",密码为空的情况。oci8.privileged_connect可为On或Off。OCI_CRED_EXT
可与OCI_SYSOPER
或OCI_SYSDBA
模式组合使用。OCI_CRED_EXT
由于安全的原因不支持 Windows 系统。
返回值
Returns a connection identifier or FALSE
on error.
注释
Note: Starting with PHP 5.1.2 and PECL oci8 1.1, the lifetime and maximum number of persistent Oracle connections can be tuned by setting the following configuration values:oci8.persistent_timeout,oci8.ping_interval and oci8.max_persistent.
Note:
In PHP versions before 5.0.0 you must use ociplogon() instead.在当前版本中,旧的函数名还可以被使用,但已经被废弃并不建议使用。
参见
oci_connect()
建立一个到 Oracle 服务器的连接oci_new_connect()
建定一个到 Oracle 服务器的新连接
[Editor's note: OCI8 1.3 should not experience the problem described in this user comment. The first use of such a connection will return an Oracle error which will trigger a cleanup in PHP. Subsequent persistent connection calls will then succeed. For high availability you might consider doing consecutive oci_pconnect calls in your script.] If you connect using oci_pconnect and the connection has logged you off but is still valid, there seems to be no way to re-use that connection. The next time I try oci_pconnect and then perform an oci_execute operation, I get a "ORA-01012: not logged on" warning. This problem remains, even if I close the connection using oci_close. I ended up with the following (rather annoying) code. <?php function getOracleConnection() { if (!function_exists('oci_pconnect')) return false; $toReturn = oci_pconnect('user', 'pass', 'db'); if ($testRes = @oci_parse($toReturn, 'SELECT Count(group_type_code) FROM pvo.group_type')) if (@oci_execute($testRes)) if (@oci_fetch_array($testRes)) return $toReturn; oci_close($toReturn); if (!function_exists('oci_connect')) return false; $toReturn = oci_connect('user', 'pass', 'db'); if ($testRes = @oci_parse($toReturn, 'SELECT Count(group_type_code) FROM pvo.group_type')) if (@oci_execute($testRes)) if (@oci_fetch_array($testRes)) return $toReturn; oci_close($toReturn); if (!function_exists('oci_new_connect')) return false; $toReturn = oci_new_connect('user', 'pass', 'db'); if ($testRes = @oci_parse($toReturn, 'SELECT Count(group_type_code) FROM pvo.group_type')) if (@oci_execute($testRes)) if (@oci_fetch_array($testRes)) return $toReturn; oci_close($toReturn); return false; } ?>
Installed on CentOS 6.2, and had lots of trouble getting it to recognize tnsnames.ora. The fix for me was: 1. Make sure apache is getting the TNS_ADMIN env variable by putting it in the /etc/init.d/httpd file: TNS_ADMIN=/usr/lib/oracle/11.2/client64/network/admin export PATH TNS_ADMIN This can be debugging in PHP by <?php echo system('env'); ?> and by verifying that TNS_ADMIN is there. 2. Make sure to use the name at the beginning of the tnsnames.ora file - not the SID (although ideally they should match. However, if the name at the beginning is XXXX.world then pconnect will expect this - not the SID)