问题:
将mysql连接或memcache连接变量直接unset()或赋值为null,连接会立即关闭吗?
测试脚本:
-
<?php
-
$m = new Memcache();
-
$m->connect("10.55.38.18", 11211);
-
//unset($m);
-
$m = null;
-
sleep(20);
执行命令:
strace php test.php
———————————————————–
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3
fcntl64(3, F_GETFL) = 0x2 (flags O_RDWR)
fcntl64(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0
connect(3, {sa_family=AF_INET, sin_port=htons(11211), sin_addr=inet_addr("10.55.38.18")}, 16) = -1 EINPROGRESS (Operation now in progress)
poll([{fd=3, events=POLLIN|POLLOUT|POLLERR|POLLHUP}], 1, 1000) = 1 ([{fd=3, revents=POLLOUT}])
getsockopt(3, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
fcntl64(3, F_SETFL, O_RDWR) = 0
close(3) = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({20, 0}, 0xbfa1e114)
————————————————————————-
测试结果:
对于一个资源,使用unset()或 设置为null,都会立即关闭连接的。
大概关闭连接的操作是在引用变为0时触发的,具体实现还没看呢。