[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;
}
?>
oci_pconnect
(PHP 5, PECL OCI8 >= 1.1.0)
oci_pconnect — Connect to an Oracle database using a persistent connection
설명
Creates a persistent connection to an Oracle server and logs on.
Persistent connections are cached and re-used between requests, resulting in reduced overhead on each page load; a typical PHP application will have a single persistent connection open against an Oracle server per Apache child process (or PHP FastCGI/CGI process). See the Persistent Database Connections section for more information.
인수
- username
-
The Oracle user name.
- password
-
The password for username.
- connection_string
-
접속을 위해 오라클 인스턴스를 포함합니다. » Easy Connect string이나 tnsnames.ora 파일의 Connect Name, 로컬 오라클 인스턴스의 이름일 수 있습니다.
지정하지 않으면, PHP는 접속할 오라클 인스턴스를 찾기 위해 TWO_TASK(리눅스)나 LOCAL(윈도우), 그리고 ORACLE_SID 등의 환경 변수를 사용합니다.
Easy Connect naming 방식을 사용하려면, PHP가 오라클 10g 이상의 클라이언트 라이브러리를 사용해야 합니다. 오라클 10g에서 Easy Connect string 형식은: [//]host_name[:port][/service_name] 입니다. 오라클 11g에서는: [//]host_name[:port][/service_name][:server_type][/instance_name] 입니다. 서비스 이름은 데이터베이스 서버에서 오라클 유틸리티 lsnrctl status를 실행하여 찾을 수 있습니다.
tnsnames.ora 파일은 오라클 넷 검색 경로에 있을 수 있습니다. $ORACLE_HOME/network/admin과 /etc 등을 포함합니다. TNS_ADMIN을 설정하여 $TNS_ADMIN/tnsnames.ora를 읽게 할 수도 있습니다. 웹 데몬이 이 파일에 읽기 권한을 가지도록 설정하십시오.
- character_set
-
오라클 서버 버전 9.2 이상을 사용하면, 새로운 연결에 사용할 charset 인수를 지정할 수 있습니다. 오라클 서버 < 9.2를 사용한다면, 이 인수는 무시되고, 대신 NLS_LANG 환경 변수를 사용합니다.
- session_mode
-
This parameter is available since version PHP 5 (PECL OCI8 1.1) and accepts the following values: OCI_DEFAULT, OCI_SYSOPER and OCI_SYSDBA. If either OCI_SYSOPER or OCI_SYSDBA were specified, this function will try to establish privileged connection using external credentials. Privileged connections are disabled by default. To enable them you need to set oci8.privileged_connect to On.
PHP 5.3 (PECL OCI8 1.3.4) introduced the OCI_CRED_EXT mode value. This tells Oracle to use External or OS authentication, which must be configured in the database. The OCI_CRED_EXT flag can only be used with username of "/" and a empty password. oci8.privileged_connect may be On or Off.
OCI_CRED_EXT may be combined with the OCI_SYSOPER or OCI_SYSDBA modes.
OCI_CRED_EXT is not supported on Windows for security reasons.
반환값
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. The old function name can still be used in current versions, however it is deprecated and not recommended.
참고
- oci_connect() - Connect to an Oracle database
- oci_new_connect() - Connect to the Oracle server using a unique connection
If oci_pconnect() fails it raises a PHP warning.
