Home  >  Article  >  Backend Development  >  How to set the timeout for PHP to connect to the Oracle database

How to set the timeout for PHP to connect to the Oracle database

PHPz
PHPzOriginal
2023-03-31 11:01:231130browse

When PHP connects to the Oracle database, if the amount of data queried is relatively large, or the query time is relatively long, it is very likely to cause the connection to time out. In order to avoid this situation from happening, we need to set the timeout period for PHP to connect to the Oracle database.

The following describes how to set the timeout for PHP to connect to the Oracle database:

  1. Set the session timeout for the Oracle database

The Oracle database has a default The session timeout is 15 minutes, and Oracle automatically disconnects if no operations are performed during this period. If you need to set a shorter session timeout, you can use the following SQL statement:

ALTER PROFILE default LIMIT idle_time 10;

The above command sets the session timeout to 10 minutes. This command only needs to be executed once, and then all connections will use this session timeout.

  1. Set the connection timeout through the PHP.ini file

Find the PHP configuration file php.ini and find the following configuration items to set the timeout for PHP to connect to the Oracle database :

; Default timeout for socket based streams (seconds)
default_socket_timeout = 600

The above command sets the connection timeout to 600 seconds (10 minutes). If you need to set a shorter connection timeout, you can change it to the corresponding value. Please note that this setting affects all scripts that use PHP to connect to Oracle databases.

  1. Set the connection timeout through PDO

If you are using PDO to connect to the Oracle database, you can set the connection timeout through the following code:

$pdo = new PDO('oci:dbname=//localhost:1521/orcl', $username, $password, array(
    PDO::ATTR_TIMEOUT => 10));

The above command sets the connection timeout to 10 seconds. If no timeout is required, this option can be set to 0.

Summary

Setting the timeout period for PHP connection to the Oracle database is a very practical technique, which can avoid connection timeout when querying a large amount of data or the query time is long. Through the above three methods, you can easily set the connection timeout to meet your needs.

The above is the detailed content of How to set the timeout for PHP to connect to the Oracle database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn