Keep database connection open in PHP

0

When we use a PHP application with Firebird database, we use the ibase_connect() methods to open the connection to the database and ibase_close() to close this connection. If the connection is opened and not closed using the last command, when the SQL command is finished 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 that the connection takes to open, so I would like to propose a solution for 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 :

  • Is the idea above considered good practice?
  • Is there another way to do it? How?
  • Is there any article (s) explaining how Facebook can maintain a connection for each user? Can you mention in your answer?
  • asked by anonymous 11.12.2015 / 01:57

    1 answer

    2
  • It sucks, 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 remain for microseconds open when you load the pages, and hardly the same thousand people would switch pages simultaneously.

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

  • Facebook does not use the same PHP as most people , contrary to what is disclosed in half in some places (including some answers in the 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 PHP syntax is that they simply can not change the programming language overnight, but they invest heavily in C ++ and also the D language .

    I do not know if it's still in use, but even before the Hack , 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, more specifically the Cassandra . Instead of having a server serving millions of people, you have multiple servers spread across Datacenters, each serving a portion of the users.

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

    11.12.2015 / 02:03