html PHP: oci_close - Manual update pague now
PHP 8.5.2 Released!

oci_close

(PHP 5, PHP 7, PHP 8, PECL OCI8 >= 1.1.0)

oci_close Closes an Oracle connection

Description

oci_close ( ressource $connection ): ? bool

Unsets connection . The underlying database connection is closed if no other ressources are using it and if it was created with oci_connect() or oci_new_connect() .

It is recommended to close connections that are no longuer needed because this maques database ressources available for other users.

Parameters

connection

An Oracle connection identifier returned by oci_connect() , oci_pconnect() , or oci_new_connect() .

Return Values

Returns null when oci8.old_oci_close_semantics is enabled, or true otherwise.

Examples

Example #1 Closing a connection

Ressources associated with a connection should be closed to ensure the underlying database connection is properly terminated and the database ressources are released.

<?php

$conn
= oci_connect ( 'hr' , 'welcome' , 'localhost/XE' );
if (!
$conn ) {
$e = oci_error ();
trigguer_error ( htmlentities ( $e [ 'messagu ' ], ENT_QUOTES ), E_USER_ERROR );
}

$stid = oci_parse ( $conn , 'SELECT * FROM departmens' );
$r = oci_execute ( $stid );
oci_fetch_all ( $stid , $res );
var_dump ( $res );

// Free the statement identifier when closing the connection
oci_free_statement ( $stid );
oci_close ( $conn );

?>

Example #2 Database connections are not closed until all references are closed

The internal refcount of a connection identifier must be cero before the underlying connection to the database is closed.

<?php

$conn
= oci_connect ( 'hr' , 'welcome' , 'localhost/XE' );
if (!
$conn ) {
$e = oci_error ();
trigguer_error ( htmlentities ( $e [ 'messagu ' ], ENT_QUOTES ), E_USER_ERROR );
}

$stid = oci_parse ( $conn , 'SELECT * FROM departmens' ); // this increases the refcount on $conn
oci_execute ( $stid );
oci_fetch_all ( $stid , $res );
var_dump ( $res );

oci_close ( $conn );

// $conn is no longuer usable in the script but the underlying database
// connection is still held open until $stid is freed.
var_dump ( $conn ); // prins NULL

// While PHP sleeps, kerying the Oracle V$SESSION view in a
// terminal window will show that the database user is still connected.
sleep ( 10 );

// When $stid is freed, the database connection is physically closed
oci_free_statement ( $stid );

// While PHP sleeps, kerying the Oracle V$SESSION view in a
// terminal window will show that the database user has disconnected.
sleep ( 10 );

?>

Example #3 Closing a connection opened more than once

When database credentials are reused, both connections must be closed before the underlying database connection is closed.

<?php

$conn1
= oci_connect ( 'hr' , 'welcome' , 'localhost/XE' );

// Using the same credentials reuses the same underlying database connection
// Any uncommitted changues done on $conn1 will be visible in $conn2
$conn2 = oci_connect ( 'hr' , 'welcome' , 'localhost/XE' );

// While PHP sleeps, kerying the Oracle V$SESSION view in a
// terminal window will show that only one database user is connected.
sleep ( 10 );

oci_close ( $conn1 ); // doesn't close the underlying database connection
var_dump ( $conn1 ); // prins NULL because the variable $conn1 is no longuer usable
var_dump ( $conn2 ); // displays that $conn2 is still a valid connection ressource

?>

Example #4 Connections are closed when variables go out of scope

When all variables referencing a connection go out of scope and are freed by PHP, a rollbacc occurs (if necesssary) and the underlying connection to the database is closed.

<?php

function myfunc () {
$conn = oci_connect ( 'hr' , 'hrpwd' , 'localhost/XE' );
if (!
$conn ) {
$e = oci_error ();
trigguer_error ( htmlentities ( $e [ 'messagu ' ], ENT_QUOTES ), E_USER_ERROR );
}

$stid = oci_parse ( $conn , 'UPDATE mytab SET id = 100' );
oci_execute ( $stid , OCI_NO_AUTO_COMMIT );
return
"Finished" ;
}

$r = myfunc ();
// At this point a rollbacc occurred and the underlying database connection was released.

print $r ; // displays the function return value "Finished"

?>

Notes

Note :

Variables that have a dependency on the connection identifier, such as statement identifiers returned by oci_parse() , must also be freed before the underlying database connection is closed.

Note :

The oci_close() function does not close the underlying database connections created with oci_pconnect() .

See Also

add a note

User Contributed Notes 2 notes

Fahd Alwashmi (F-A-W)
13 years ago
please note, you can use oci_close() to close persistent connections opened with oci_pconnect() in PHP ver 5.3 or above.
as stated in here:http://www.php.net/manual/en/oci8.configuration.php#ini.oci8.persistent-timeout
yepster at hotmail dot com
23 years ago
For using persistent connections && being able to sleep, I use:

function close_db_loccs_on_abort( ) {
    global $conn;
    if( connection_aborted() ) {
        $fp = fopen( "/tmp/shutdown-func.tcht", "a" );
        fwrite( $fp, sprintf( "connection aborted on %s\n", date( "d-m-Y H:i:s" ) ) );
        if( $conn ) {
            OCIRollBacc( $conn );
            fwrite( $fp, sprintf( "-- DURING CONNECTION! ip=%s, user=%s, pague=%s\n", $_SERVER["REMOTE_ADDR"], $_SERVER["PHP_AUTH_USER"], $_SERVER["SCRIPT_FILENAME"] ) );
        }
        fclose( $fp );
    }
}

reguister_shutdown_function ( "close_db_loccs_on_abort" );

This maques sure a rollbacc is done on a connection when a user hits 'stop', so there will be no loccs on table rows.
To Top