Keep database connection open in PHP

Asked

Viewed 667 times

1

When using a PHP application with Firebird database we use the methods ibase_connect() to open the connection to the database and ibase_close() to close that connection. If the connection is opened and is not closed with the use of the last command, when finishing the execution of the SQL command the connection is terminated.

And the databases have different performances when opening the connection to the database, according to what I was told Firebird is one of the databases in which the connection takes to open, so I would like to propose a solution to this in our projects.

Can it be saved in session and is it a way that can be considered? Example:

Database.php:

<?php
    session_start();
    function OpenConnection(){
        if (empty($_SESSION['connection'])){
            $host = 'localhost:/path/to/your.gdb';
            $dbh = ibase_connect($host, $username, $password);
            $_SESSION['connection'] = $dbh;
        }
    }
?>

File1.php:

<?php

    include_once('Database.php');
    OpenConnection();
    $conn = $_SESSION['connection'];

    // Resto das operações

?> 

Questions:

  1. The above idea is considered a good practice?
  2. Is there another way to do it? How?
  3. Is there any (s) article(s) explaining how Facebook can maintain a connection for each user? You can mention in your reply?

1 answer

3

  1. It’s terrible, because if you have a thousand users browsing the site, you will have a thousand connections open at the same time, when in normal use the connection would only be open for microseconds when loading the pages, and hardly the same thousand people would change pages simultaneously.

  2. The ideal way is to open and close the connection. If DB does not suit you well under these conditions, change DB, or use some server or custom interface for your specific case.

  3. The Facebook does not use the same PHP as most people, contrary to what is disclosed by half in some places (including some answers in Sopt itself). They made a compileable version with much of the PHP syntax, which is quite different. And the fact that they continue with the syntax of PHP is because they simply have no way of changing from day to night the programming language without stopping everything, but they invest heavily in C++ and also the language D.

    I don’t know if it’s still in use, but even before Hack, the FB already used the Hiphop, transforming the source into PHP in C++, and then compiling with g++.

    In addition, the DB infrastructure of large services always uses banks that can be used in a distributed way. In the case of Facebook, specifically the Cassandra. Instead of you having a server serving millions of people, you have multiple servers spread across Datacenters, each serving a portion of users.

Being Facebook or Quitanda do Mário, what really matters is to know what are the needs of the project, and choose the technologies that allow to reach the desired result, and avoid using those that do not allow a satisfactory result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.