What exactly is a MySQL connection?

Source: Internet
Author: User
$db = new PDO ()
At the bottom, what is the generated DB instance, exactly?

Under Linux Everything under the document concept, I think, this instance should also be a file.
1. What information is stored in this file?

Suppose such a scenario:

$db = new PDO(...)while(true){sleep(90000)    $db->xx()}

DB connection is inactive for a long time, MySQL will automatically disconnect, after disconnecting, PHP to request will report MySQL server has gone away error.

2, how to judge before $db->xx () This db has been disconnected?

Reply content:

$db = new PDO ()
At the bottom, what is the generated DB instance, exactly?

Under Linux Everything under the document concept, I think, this instance should also be a file.
1. What information is stored in this file?

Suppose such a scenario:

$db = new PDO(...)while(true){sleep(90000)    $db->xx()}

DB connection is inactive for a long time, MySQL will automatically disconnect, after disconnecting, PHP to request will report MySQL server has gone away error.

2, how to judge before $db->xx () This db has been disconnected?

    1. What information does this file hold?
      I didn't find the file either, the instance object is in memory.

    2. How can I tell if this db has been disconnected before $db->xx ()?
      You can use exceptions to throw:


  
   query('SELECT * from FOO') as $row) {        print_r($row);    }    $dbh = null;} catch (PDOException $e) {    print "Error!: " . $e->getMessage() . "
"; die();}?>

PHP provides persistent connections
Persistent connection caching avoids the overhead of creating a new connection each time the script needs to respond to the database, making the Web application faster.


  
    true));?>

RESOURCE

MySQL causes this behavior where there are two interactive_timeout, wait_timeout
You can extend the time by modifying the MySQL configuration file/etc/my.cnf. This is set for 10 seconds.
[Mysqld]

wait_timeout=10interactive_timeout=10

The workaround is to:
Change these two parameters to:

wait_timeout=100interactive_timeout=100

After restarting MySQL server entry, the view settings are in effect.
You give it a try.

Essentially, the link to MySQL is a common to socket socket, so it's also a File Descriptor . Since it is a link tcp , the link between the two parties has the right to disconnect at any time, as the big God Upstairs said, this is generally defined by the mysql server link timeout time, this is to prevent excessive links caused by mysql resource consumption.

In general php , what is not required sleep , normal use of the pdo object does not take into account the link timeout, pdo will maintain everything.

if($db){ $db->xx() }

All documents, refer to official documentation, general knowledge

Essentially a piece of memory buffer

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.