[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
Opis
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.
Parametry
- username
-
The Oracle user name.
- password
-
The password for username .
- connection_string
-
Contains the Oracle instance to connect to. It can be an » Easy Connect string, or a Connect Name from the tnsnames.ora file, or the name of a local Oracle instance
If not specified, PHP uses environment variables such as TWO_TASK (on Linux) or LOCAL (on Windows) and ORACLE_SID to determine the Oracle instance to connect to.
To use the Easy Connect naming method, PHP must be linked with Oracle 10g or greater Client libraries.
- character_set
-
Używając serwera Oracle 9.2 lub nowszej wersji można podać parametr charset , który zostanie użyty przy nowym połączeniu. Używając serwera Oracle < 9.2 ten parametr zostanie zignorowany, a zamiast niego użyta zostanie zmienna środowiskowa NLS_LANG.
- session_mode
-
This parameter is available since version 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 and 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.
Zwracane wartości
Returns a connection identifier or FALSE on error.
Notatki
Informacja: 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.
Informacja: 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.
Zobacz też:
- oci_connect() - Connect to an Oracle database
- oci_new_connect() - Connect to the Oracle server using a unique connection
oci_pconnect
09-Oct-2008 04:38
21-Nov-2002 06:04
If your oracle database is on a remote system within your local network and you don't want to worry about the tnsnames file you can try this.
$db = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.XX.XXX)(PORT = 1521)))(CONNECT_DATA=(SID=XXXX)))";
$c1 = ocilogon("name","password",$db);
Hope this helps someone.
26-Sep-2002 06:09
Better to insert needed variables into apache(!) (or your webserver - respectively) start script.
For example on UNIX systems /etc/init.d/apache would contain following lines before anything else:
#!/bin/bash
if [ -f ~oracle/.profile ]; then
source ~oracle/.profile
fi
The ~oracle/.profile has the appropiate settings to start an Oracle database so it is always up-to-date for PHP, too. (If settings are changed, don't forget to restart your webserver.) This way you just no need to worry what to include or define for PHP.
