oci_close

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

oci_close — Oracle 연결을 닫습니다.


설명

oci_close(resource $connection): ?bool

connection을 해제합니다. 기본 데이터베이스 연결은 다른 리소스가 사용하지 않고 oci_connect() 또는 oci_new_connect()로 생성된 경우 닫힙니다.

더 이상 필요하지 않은 연결은 닫는 것이 좋습니다. 이렇게 하면 다른 사용자가 데이터베이스 리소스를 사용할 수 있기 때문입니다.


매개변수

connection
oci_connect(), oci_pconnect() 또는 oci_new_connect()에서 반환된 Oracle 연결 식별자입니다.

반환 값

oci8.old_oci_close_semantics가 활성화되면 null을 반환하고 그렇지 않으면 true를 반환합니다.


Examples

예제 #1 연결 닫기

기본 데이터베이스 연결이 제대로 종료되고 데이터베이스 리소스가 해제되도록 연결과 관련된 리소스를 닫아야 합니다.

                  
<?php

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

$stid = oci_parse($conn, 'SELECT * FROM departments');
$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);

?>
                  
                

예제 #2 모든 참조가 닫힐 때까지 데이터베이스 연결이 닫히지 않습니다.

연결 식별자의 내부 참조 횟수는 데이터베이스에 대한 기본 연결이 닫히기 전에 0이어야 합니다.

                 
<?php

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

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

oci_close($conn);

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

// While PHP sleeps, querying 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, querying the Oracle V$SESSION view in a
// terminal window will show that the database user has disconnected.
sleep(10);

?>
                 
               

예제 #3 두 번 이상 열린 연결 닫기

데이터베이스 자격 증명을 재사용하는 경우 기본 데이터베이스 연결을 닫기 전에 두 연결을 모두 닫아야 합니다.

                
<?php

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

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

// While PHP sleeps, querying 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);  // prints NULL because the variable $conn1 is no longer usable
var_dump($conn2);  // displays that $conn2 is still a valid connection resource

?>
                
              

예제 #4 변수가 범위를 벗어나면 연결이 닫힙니다.

연결을 참조하는 모든 변수가 범위를 벗어나 PHP에 의해 해제되면 롤백이 발생하고(필요한 경우) 데이터베이스에 대한 기본 연결이 닫힙니다.

               
<?php

function myfunc() {
    $conn = oci_connect('hr', 'hrpwd', 'localhost/XE');
    if (!$conn) {
        $e = oci_error();
        trigger_error(htmlentities($e['message'], 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 rollback occurred and the underlying database connection was released.

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

?>
                 
               

노트

메모: oci_parse()에 의해 반환된 명령문 식별자와 같이 연결 식별자에 종속성이 있는 변수도 기본 데이터베이스 연결이 닫히기 전에 해제되어야 합니다.

메모: 버전 PHP 5.1.2(PECL OCI8 1.1) 이전에는 oci_close()가 작동하지 않았습니다. 최신 버전에서는 Oracle 연결을 올바르게 닫습니다. 이 함수의 이전 동작을 복원하려면 oci8.old_oci_close_semantics 옵션을 사용하십시오.

메모: oci_close() 함수는 oci_pconnect()로 생성된 기본 데이터베이스 연결을 닫지 않습니다.


기타