一、导语
连接池在JAVA中应用的很广泛,而在PHP中很少使用。
以Mysql为例,JAVA中使用连接池的原因之一是避免重复新建、释放连接资源带来的开销。而在PHP中都是直连,因为这部分开销对于C API而言性能上是没有问题的。
那么既然PHP直连已经没有性能问题,那为何还要多次一举开发mysql的数据库连接池扩展呢?因为根本的目的是在于理解PHP的TS(Tthread Safe线程安全)与NTS(Not Thread Safe非线程安全)运行模式,而并非数据库连接池这个功能。
二、原理连接池的基本思想是在系统加载时,初始化默认数量的连接对象存储在内存中,当有客户端需要访问数据库时,根据场景选择是分配、新建、等待、失败返回连接对象。使用完毕之后,连接将被重新放置回连接池中等待下一个请求的再分配,而不是释放内存。
连接池中连接的建立、释放都由连接池自身来管理,同时可以通过设置初始化最小连接数、最大连接数、最大空闲时间等来配置连接池。
注:在此扩展中,提供了最小连接数(min_connection)、最大连接数(max_connection)的设置
三、实现思路1.定义全局数组变量dbpools
dbpools中的元素类型为
struct _mysql_node{ MYSQL *mysql; //连接资源句柄 int is_used; //标记是否被占用 int result_id; //记录my_mysql_query的查询结果集} mysql_node;
2.通过获取配置文件中设置的min_connection初始化dbpools
3.定义全局变量db_pools_length(目前拥有链接数)、db_pools_use_length(目前被使用的链接数),通过这两个值来确定分配资源的情景
注:正因为使用全局变量实现连接池,通过修改全局变量的状态来选择资源连接,决定了该扩展必须运行在共享全局变量的ZTS(Zend Thread Safe)线程安全模式下。(例如IIS或Apache MPM Worker模式)
如果希望支持多进程,可以通过进程间通信来设置全局变量,实现线程池。
实现思路图:
图片:1.jpg
四、理解PHP线程与进程的工作模式(一)多进程工作模式
PHP的多进程工作模式以Apache apxs举例。
apache启动时,会fork出N个子进程用来等待接受处理客户端的请求。进程之间相互隔离,全局变量也无法直接访问(可以通过进程间通信访问)。这样的好处是能够保证PHP环境的长时间稳定,
即使有部分进程因内存泄漏而崩溃也不会影响其他进程。由于PHP相当于粘合剂,它实际相当于集合多个库的API,例如集合了libcurl、libmemcache、libzip等,要保证所有的库都正常运行是比较困难的。
那么为了保证PHP的高可靠性,这种多进程的模式就是首选。
图片:2.jpg
(二)多线程工作模式
PHP的多线程工作模式以IIS举例。
这种模式下,只有一个进程在后台运行着,所有请求都是通过这一个进程来完成。只是在处理每个请求时,会创建独立的线程来处理。
正是因为使用多线程这种模式,可以直接访问全局变量得以方便的实现数据库连接池。
图片:3.jpg
六、使用此扩展前提条件与方法1.widnwos下IIS Server或Apache多线程模式,php5.3.*
2.修改php.ini,添加
[my_mysql]
my_mysql.host = localhost
my_mysql.user = root
my_mysql.password =
my_mysql.port = 3306
my_mysql.max_connection = 200 //最大连接数
my_mysql.min_connection = 100 //默认最小连接数
看到这里的配置,能看出来这个数据库连接池扩展没有实现多数据源的连接池。因为目的不在连接池本身,所以也没有特地去写多数据源的功能。
3.修改php.ini,添加
extension=php_my_mysql.dll
4.重启apache server
七、相关下载扩展dll下载: php_my_mysql.dll
源码下载: http://yunpan.cn/QWmEN8PuuRVYN
扩展测试结果测试配置:
[my_mysql]
my_mysql.max_connection = 2 //最大连接数
my_mysql.min_connection = 1 //默认最小连接数
使用三个浏览器,运行下列测试脚本:
<?php/*** 从数据库连接池中取得一个链接资源* 可能产生如下情景* 1.如果有空闲连接则直接返回链接资源* 2.如果没有空闲连接,并且连接数没有超过最大连接数,则扩充连接池并返回新建的链接资源* 3.如果没有空闲连接同时已经是最大连接数,则进入等待,超过1.5s仍没有空闲资源则超时返回NULL.** 失败返回NULL*/$db = my_mysql_get_conn(); if($db === false){ exit('dbpool is exhushed.');} /*** 选择数据库* @param resource $db* @param string $db_name 数据库名称*/my_mysql_select_db($db, 'test'); /*** ping数据库*/my_mysql_ping($db); /*** 执行SQL语句,可以执行INSERT、SHOW、SELECT、UPDATE、DELETE等语句* @param resource $db* @param string $sql SQL*/my_mysql_query($db, "INSERT INTO test VALUES(id, 'dbpool', 'test dbpool')"); // 获取上一条INSERT语句返回的自增ID$id = my_mysql_get_insert_id($db);echo $id; $result = my_mysql_query($db, "SELECT * FROM test"); /*** 将查询结果转换为二维数组* @param resource $db*/$arr = my_mysql_fetch_assoc($result); print_r($arr); sleep(3); var_dump($db);//sleep之后,必须输出$db,否则会因为PHP的解析优化,sleep过程中已经将$db释放,就没有办法测试连接被占用的情况。
测试结果图:
图片:4.jpg
返回warning的,是由于连接池资源被占用完,会进入等待。如果1.5s仍没有等待到资源,则返回dbpool is exhushed。
Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
