web mysql数据库的持久连接,连接池与持久连接mysqli

I've read that mysqli introduced connection pooling that was not available in mysql.

http://php.net/manual/en/mysqli.quickstart.connections.php

I've read this question that explains the difference between connection pooling and persist connections.

If a unused persistent connection for a given combination of host,

username, password, socket, port and default database can not be found

in the connection pool, then mysqli opens a new connection.

So, does mysqli use connection pooling for persistent connections?

If yes, then what is difference between connection pooling and persistent connections in mysqli?

If no, then how does mysqli perform a lookup for a connection object for a persistent connection?

解决方案

Actually terms connection pooling and persistent connection refer to the same thing in case of mysqli in PHP.

Persistent connection in this case refers to MySQL connection open from PHP script which stays open after the script has finished executing, to be used again in some later executions.

Connection pooling means that there is a pool of persistent connections maintained by PHP. One idle connection from this pool is given to PHP script which wants to connect to MySQL and returned to pool when script finishes.

You might wonder why do we need the pool of MySQL connections at all, why don't we use just one persistent connection for all of the scripts?

There are two reasons for this:

PHP creates a pool of MySQL connections based on host/port/username/password used. If one script wants to connect to MySQL with some host/port/username/password combination, PHP searches for idle persistent connection which has the same values. If it's not found, then a new persistent connection is created with this host/port/username/password combination. So we need at least as many different persistent connection as there are different host/port/username/password values used by all of the scripts.

You cannot execute two SQL commands on one MySQL connection at the same time. This can happen when two PHP scripts are executing simultaneously. When two scripts want to communicate with MySQL at the same time, two persistent MySQL connections are created. Number of persistent connections in pool is equal to last number of maximum parallel PHP scripts executed, or equal to upper limit set in php.ini.

Important notice:

MySQL connection pools (and any other connection pools) can exist only if PHP is executing as a web server plugin. Pools do not work when it is working in fast-cgi mode or in any other way when PHP executable terminates after script execution.

Edit: MySQL connection pooling can be used in fast-cgi mode of PHP if web server is configured to reuse one PHP fast-cgi process for multiple requests. If PHP fast-cgi process is configured to exit after serving one request then all of it's MySQL connections are closed.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值