Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:90804 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 762 invoked from network); 21 Jan 2016 19:20:47 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Jan 2016 19:20:47 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.50 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.50 mail-wm0-f50.google.com Received: from [74.125.82.50] ([74.125.82.50:34974] helo=mail-wm0-f50.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A5/50-09073-E8F21A65 for ; Thu, 21 Jan 2016 14:20:46 -0500 Received: by mail-wm0-f50.google.com with SMTP id r129so185354010wmr.0 for ; Thu, 21 Jan 2016 11:20:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=+uAbJRw8q9bjO0CEUNcaNsI1OvBeTM302kMNILMM4zI=; b=EyCE/eObAas5iEW6mk2gEk1YPowjHDzxgXDr5pFh1Sd7DXk1Jms1fpqwwei5OPKtdQ GIOpIO4hbUcJ9ABe9vZEuG/XCpmY1lND+eDglvWcWjZLLPkXyn7+LEOqltfDBIYSisRB BwrYUTdE4sEFQs+NpUqPCI6rIJxZtKwKDDvdPruDLHRk7vNDS6Bqb22odeDZpoUc+ZMx 6orwK5Bll5cOjp3tFsqd+RxOHemLR+5A62sJe3CBpyxU5tTC5RSqoRW2O7bFpYFBApb2 NH8B75HcBb9fYaYbkOCaE9isVZGm8FSuAl0J5fI8PxE0F1sW9NKu3/U4xPP0unLyyOB7 6OSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-type :content-transfer-encoding; bh=+uAbJRw8q9bjO0CEUNcaNsI1OvBeTM302kMNILMM4zI=; b=jZ1Pkg4ZNLiafDwVzGo1+CIeEzlFh83B5uDcyzAMW+duhhPSW6SXMOTXlUfbS+ct86 XnGbmLcO5T1l8GSBiGYHPUFTs4zMxDN9TBHtrE165rd6nTy69rt36MrWHFXaO2GoVoFi WSyCCEN2B4g6D21dwaqr0QRADkPYvW2jt1a6ehtEjN3ovkbRRLANVaiBIfruDrRKsys8 uBeQycxBt989rgxeMHZtmBFWcK3/49o2/CJkWzsQud/B0nuFQv+5GrD4LKYnuTFmoTj3 kx0GOxG/bHECMVSUfHgvol7b7wGQYI+Bqtb5q0ZFh9byDht1Hsg94AXaxwMCsdkDVebu 1bXw== X-Gm-Message-State: ALoCoQnq76LgSvaboIJdRaofGsZnzMGlBW3Z7E+F+by84hemRgOUKQLFudNkZ1K/L5J3BdN6+alV4ReH0fq4VpTkVwH1ifE6/g== X-Received: by 10.194.71.77 with SMTP id s13mr52114204wju.175.1453404043430; Thu, 21 Jan 2016 11:20:43 -0800 (PST) Received: from [192.168.0.177] ([93.188.182.58]) by smtp.googlemail.com with ESMTPSA id t199sm4206291wmt.23.2016.01.21.11.20.42 for (version=TLSv1/SSLv3 cipher=OTHER); Thu, 21 Jan 2016 11:20:42 -0800 (PST) To: internals@lists.php.net References: Message-ID: <56A12F1B.9080708@gmail.com> Date: Thu, 21 Jan 2016 19:18:51 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] PDO Close Connection From: rowan.collins@gmail.com (Rowan Collins) Gabriel Zerbib wrote on 21/01/2016 14:32: > Hi, list! > > This is my first email to this mailing list, so please pardon me for misuse > of the traditions you may have here (formatting, phrasing, etc.). Welcome! :) > Maybe this has been discussed in depth earlier, but: I want to ask if there > is an explicit reason for not giving a "close" instance method in the PDO > class. I think the main problem with a close method on something that represents a resource like a PDO connection object is how the object should behave *after* it has been closed. That is, what should the following code do? $pdo = new PDO($dsn); $pdo->closeConnection(); $pdo->query('Select 1 as test'); The object needs to track the fact that it is in a closed state, and do something - maybe throw a ConnectionAlreadyClosedException? But then what should the calling code do about that? If you have many objects with reference to that PDO object, then any one of them could "close" it, and any one could then try to execute a query. If you're not careful, your closeConnection() method could become a killScriptNextTimeAQueryIsAttempted() method. If what you are actually doing is putting the connection to sleep, to be "woken up" later, you need *all* the references to the object to have the ability to "wake it up", because if you just create a new connection, you've somehow got to propagate it to all the same places you'd have to unset() in the current implementation. So that means the close() method needs to be accompanied by a reopen() method, which means saving the DSN and credentials, and any initialisation calls you want to make at the beginning of the connection (setting encoding, etc)... > In an application made of dependency injection, closures and many levels of > function calls split over many files, it is virtually impossible to keep > reference counting mentally, and you mostly end up never knowing what other > component of the application still retains a reference to the PDO object. One solution to this is to create a wrapper around PDO, which is the only thing allowed to have a direct reference to the PDO object itself. To this object, you can add a close() method to your wrapper, and maybe isOpen(). If you save the DSN as well as the connection object, you can then go further and have reopen(), openOnNextQuery() (lazy-loading), or have your implementation of query() silently ensure that a connection is open. You could have a hook that fires on connection / disconnection to notify other objects, a list of SQL calls to run on connection, etc. etc. That's not to say that a simple implementation of just close-and-fire-exception would be a horrible idea in PDO, but having one object which represents an open connection, and a separate one which represents a *potential* connection (maybe open, maybe closed, maybe not-open-yet) is a useful abstraction, and doesn't need anything in PDO itself. Regards, -- Rowan Collins [IMSoP]