1

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

Jabda
  • 1,752
  • 5
  • 26
  • 54

2 Answers2

3

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.

bishop
  • 37,830
  • 11
  • 104
  • 139
2

You can use oci_set_call_timeout for database call timeouts since oci8 2.2.0.

<?php

$conn = oci_connect('hr', 'welcome', 'localhost/XE');
oci_set_call_timeout($conn, 5000);

?>
Dorjee Dhondup
  • 549
  • 6
  • 15