Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128746 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 914241A00BC for ; Tue, 30 Sep 2025 15:35:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1759246454; bh=tOszGBLi+LQckqlSMg5fOxDlT9soLbdRUSh2QOjzzvs=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=UY6HTZmXRMGWzI+wTDvZ6ozPkwGNo5e4pICoKRMTpCf6tdOd6sF2tuGLIHw1e2neF EaZaidCvPGf/vqdrj91syvbe8HYdlTxUB4d9tsG+kgjCFpomnE8JCG6Heb0n7xTLDx NaBS1EPTQi+AUGx3FYL/5id5WjGaevcfKLWrXg+9REa2Q9UJFtS21jKZLTJFJum/Xc Cpt5UxSUQ/TnDfSohIy6tmCpQGrVKmZH0SHZ3ZHNC2KtFqAmfKYGw747j1IPUtD+6Z 2HtO9F8xwgs31/pIuzIEvT8148zlreuJqH0xbQ7GmjVA+sw9YGm53/8h5AfPDQkjcn qSugy2p+9UuyQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BD3FF1801DC for ; Tue, 30 Sep 2025 15:34:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,T_SPF_TEMPERROR autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 30 Sep 2025 15:34:10 +0000 (UTC) Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-afcb78ead12so1071675466b.1 for ; Tue, 30 Sep 2025 08:35:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1759246528; x=1759851328; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=tOszGBLi+LQckqlSMg5fOxDlT9soLbdRUSh2QOjzzvs=; b=fW1tpq5cTTmKpDW+3UD/UTZCLNM+HDrz0ziIE57d+3e19r/+8XgaKN7bKTN64hCfLA Oi++3sIPhkmyWDU3bha7MYrOQWw4c5Bn0ulAKjaSdYY5/KOXO+Rq8ITBOdqlrJNmlqSh bd7w2kqgjzewvln2T63zRuhRPRWbSi37vfSqRIsShqZ86tTS0qHdFhSt3/OcD7xKJkKU As8K/Vsrd3+RUsua//NcxI9NTcZLuQ8kURd+qm+PAiLKXzlSeoh9mejbWyPIYz1s1zpg TmY88+zriS8wrWTa3SANwUknHR83o2tMBzdgsaoWJrP6E5hFmJ4xtsswblmGhomvxckf 6jiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759246528; x=1759851328; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tOszGBLi+LQckqlSMg5fOxDlT9soLbdRUSh2QOjzzvs=; b=TQ9u/z8e3Ao1tfBTgO4tG5GoF6wBZ+r6UKnbYljTuspu+DOlrkZHmHZOBXq8q/w9e2 q3HYfzkNEbCvqTUHwXo/W9VHci4oekLUCpCPCpNAk4VYotEHcRNYm+V0k21jTFMKbNzP zd8XqHn0/XbYNDioDmIKRNWfUsGGhhlVg8FOcELVDTCSNC6GMFYskfA0gtlhMot/W+DK vz9hVCjQrBXbIoI9+yb+6n9XoVKHUo1RQj6iod6T9trB27VnONlGMfnHeL/nqd/XVjRp oHNRs9uCoZJX1mdW1W4BBvIc+bHT3yKqVNRddHKbDd+ExGOkVwhX/9fuM0sCWS3ECqEs 3CNQ== X-Forwarded-Encrypted: i=1; AJvYcCUY9JbY/BKMcVfBU6R2pchV+8Mr0q4ytWEHgSJRf8R1FFgMO6KhgtWgoRkh7Kz5F01bYGbeoSoIdbM=@lists.php.net X-Gm-Message-State: AOJu0Yz14dJWkkdZvYH7IBhL1yuixzWqGS/yau/KvYcn/DmsecvY+juL CQIXJTQgtqbV3v4jIeX7EMZsVLr10+DHrgom5lZIc3D8ui1i+8nvnOmwFzC8gZWz8Lum2Y7vGRy ryYd3tuU4ADkXhNWUOPFWQQC1HyG8rEQ= X-Gm-Gg: ASbGnct1BrRGUHXXYL2U4RVx9h48Mcc7WTvtqnWr/NjTSdSycHnRiQx9cI9h4wt4jyB vjsUtbRwBJHUKOCxpm7g2vd2Aa1iJjjrTXTbCwP/YzfxCPqdQLK2x/5EdIRaT892GdS7KdnfiAG dAmkX8tvFX7b+vgH6nI7DUlO4fQ3W+kOOZzqj847mTleq5v1HNVUZXQniN9NRf5yY2I5dOezmuu V4kJZc2dBeUxLFrHpdsJ6UXHi6bjbcljBjFREH4Dw== X-Google-Smtp-Source: AGHT+IE3xVT/V+qFSSaNua7MoHDjvQ8c1IUTk7ch7SEUQnp2XGC1OKOu1ejfcsJV765uR8B2L2N8nzqDyzJ38skORes= X-Received: by 2002:a17:907:6e8b:b0:afe:7b8c:a583 with SMTP id a640c23a62f3a-b46e4b8fc85mr7021166b.13.1759246528068; Tue, 30 Sep 2025 08:35:28 -0700 (PDT) Precedence: list list-help: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 30 Sep 2025 11:35:15 -0400 X-Gm-Features: AS18NWDKOdlLnBVLKcP1rFWKJxfq2sywD3oMiBQFSsV50WOedgEarYQCaTZVQBQ Message-ID: Subject: Re: [PHP-DEV] [Discuss] Add PDO disconnect() and isConnected() To: Kamil Tekiela Cc: Andrey Andreev , PHP internals Content-Type: multipart/alternative; boundary="00000000000060c13806400680ba" From: rposky@gmail.com (Robert Wolf) --00000000000060c13806400680ba Content-Type: text/plain; charset="UTF-8" > > I find this proposal to be a backwards step in the age when we are > moving away from resources and their explicit closure. We have > disabled this in PHP 8.0 for curl_close(), imagedestroy(), > openssl_pkey_free(), shmop_close() and xml_parser_free(). Thank you for providing these recent examples of resource-to-object conversions. As you well know, PDO is an object which also optionally uses PHP persistent resources to facilitate connection re-use across PHP requests, a feature which I don't believe has a parallel in object space. Perhaps therein lies a feature request, i.e. to allow an object to be made persistent in the same way a resource can. However, as there is no present path for PDO's conversion away from PHP persistent resources, the prior examples you highlight seem to support an explicit close interface in PDO, in that so long as PHP resources are involved, a close method is warranted. PDO was designed much > better and it deliberately avoided the foot gun that is explicit > connection closure. I'll push back on the notion that disconnect() is likely to be a foot gun. In general, a held PDO object does not guarantee an active connection to a database. Remote connection closures do occur. Networks falter and fail. A robust application necessarily accounts for connectivity failure states, and in the case of PDO, is likely validating the success of every database interaction and reacting accordingly. Considering, an implementation which unwittingly invokes disconnect() and permits continued use of the same PDO for database interactions will at least not error in novel ways. The recommendation for avoiding such mistakes in implementation would be the same which the community has long advocated for - decorate your PDOs. Maybe there are legitimate use cases for this, but > there are also alternatives. I listed a couple use cases in the RFC, which I'll quote below for some added context. * Efficient management of database resources across periods of application idleness, opening and closing connection as needed. * Interoperability with database environment imposing timeout or other session constraints, necessitating multiple connections during script execution. * Abnormal termination procedure in which database closure is highly prioritized, e.g. security violation. * Testing purposes, e.g. simulation of remote connection failure. Pretty much every time I hear the > argument that it would be nice if PDO had it, I can reply with: you > can just design your code in a way that doesn't need it. I tried responding to this in the RFC, also quoted below. PHP makes the implementation of a decorator simple enough through magic methods like __call and __get, but this is complicated in PDO by the PDO statement object, which holds reference to a PDO object, and so must also be reference-managed. A leak-proof decorator should thus override PDO::prepare() and PDO::query() to also prevent the leak of the PDO statement object, likely requiring additional interfaces to otherwise interact with prepared PDO statements, to e.g. bind parameters and execute them, and queried PDO statements, to e.g. fetch one row, N rows, one column, etc. This prescribed implementation pattern is burdensome. For persistent PDO objects, the user has no native capacity to close a connection, given that the database handle is inaccessible and registered as a PHP persistent resource, which remains until the PHP process terminates. To ensure database connectivity across subsequent PHP requests, each new PDO object includes a liveness check on the persistent connection, possibly prompting a new connection. This has meant the burden of defining a viable database connection resides within the database driver's liveness check, and that an outcome differing with actual viability as it concerns a given application results in a non-viable database connection for all subsequent PHP requests. The proposed disconnect() thus grants the user the capacity to declare a connection as no-longer viable. --00000000000060c13806400680ba Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I find this proposal to be a backwards st= ep in the age when we are
moving away from resources and their explicit = closure. We have
disabled this in PHP 8.0 for curl_close(), imagedestroy= (),
openssl_pkey_free(), shmop_close() and xml_parser_free().

Thank you for providing these recent examples of resour= ce-to-object conversions. As you well know, PDO is an object which also opt= ionally uses PHP persistent resources to facilitate connection re-use acros= s PHP requests, a feature which I don't believe has a parallel in objec= t space. Perhaps therein lies a feature request, i.e. to allow an object to= be made persistent in the same way a resource can.

However, as there is no present path for PDO's conversion away from P= HP persistent resources, the prior examples you highlight seem to support a= n explicit close interface in PDO, in that so long as PHP resources are inv= olved, a close method is warranted.

PDO was designed much
better and it deliberately avoided the foot gun = that is explicit
connection closure.

I'll= push back on the notion that disconnect() is likely to be a foot gun.
<= br>In general, a held PDO object does not guarantee an active connection to= a database. Remote connection closures do occur. Networks falter and fail.= A robust application necessarily accounts for connectivity failure states,= and in the case of PDO, is likely validating the success of every database= interaction and reacting accordingly.

Considering, an implemen= tation which unwittingly invokes disconnect() and permits continued use of = the same PDO for database interactions will at least not error in novel way= s. The recommendation for avoiding such mistakes in implementation would be= the same which the community has long advocated for - decorate your PDOs.<= /div>

Maybe there are legitimate use cases f= or this, but
there are also alternatives.=C2=A0

I listed a couple use cases in the RFC, which I'll quote below = for some added context.=C2=A0

* Efficient manageme= nt of database resources across periods of application idleness, opening an= d closing connection as needed.
* Interoperability with database environ= ment imposing timeout or other session constraints, necessitating multiple = connections during script execution.
* Abnormal termination procedure in= which database closure is highly prioritized, e.g. security violation.
= * Testing purposes, e.g. simulation of remote connection failure.
=

Pretty much every time I hear the
argume= nt that it would be nice if PDO had it, I can reply with: you
can just d= esign your code in a way that doesn't need it.=C2=A0
<= br>
I tried responding to this in the RFC, also quoted below.

P= HP makes the implementation of a decorator simple enough through magic meth= ods like __call and __get, but this is complicated in PDO by the PDO statem= ent object, which holds reference to a PDO object, and so must also be refe= rence-managed. A leak-proof decorator should thus override PDO::prepare() a= nd PDO::query() to also prevent the leak of the PDO statement object, likel= y requiring additional interfaces to otherwise interact with prepared PDO s= tatements, to e.g. bind parameters and execute them, and queried PDO statem= ents, to e.g. fetch one row, N rows, one column, etc. This prescribed imple= mentation pattern is burdensome.

For persistent PDO objects, th= e user has no native capacity to close a connection, given that the databas= e handle is inaccessible and registered as a PHP persistent resource, which= remains until the PHP process terminates. To ensure database connectivity = across subsequent PHP requests, each new PDO object includes a liveness che= ck on the persistent connection, possibly prompting a new connection. This = has meant the burden of defining a viable database connection resides withi= n the database driver's liveness check, and that an outcome differing w= ith actual viability as it concerns a given application results in a non-vi= able database connection for all subsequent PHP requests. The proposed disc= onnect() thus grants the user the capacity to declare a connection as no-lo= nger viable.=C2=A0
--00000000000060c13806400680ba--