phporacle-call-interface

PHP oci timeout?


I would like to set a timeout on a query

$statement = oci_parse($conn, $query);
oci_execute($statement); // timeout if this takes over 5 minutes

this is was asked before but all the way back in 11 with no solid answer

Run a sql query with a timeout in PHP or How in PHP, to stop a sql query once already begun?

I found this request too from last year https://bugs.php.net/bug.php?id=73038


Solution

  • The PHP OCI driver does not support query time outs (via PDO::ATTR_TIMEOUT) at the interface with the Oracle library. Thus, there is no direct user-land means to accomplish this.

    The standard way to do this would be to execute a separate process that itself has a timeout. You might go about it like so:

    // oci8-exec.php
    set_time_limit(300); // 5 minutes
    $conn = getDatabaseConnection();
    $stmt = oci_parse($conn, $_SERVER['argv'][1]);
    oci_execute($stmt);
    oci_fetch_all($stmt, $rows);
    var_export($rows);
    

    then in your code do:

    $rows = shell_exec('oci8-exec.php "select * from foo"');
    

    Obviously you would need to have error handling, exit code checking, etc. to make this production ready.