Page 3 of 3

Re: I don't know if this is Singleton but...

Posted: Sat Mar 07, 2009 5:36 pm
by kaisellgren
josh wrote:
kaisellgren wrote:Having a singleton effectively makes sure no one will ever establish multinuous connection to the database.
That is true, but why would you want to write a database library that can't support multiple connections. For example one adapater for writes and another for reads, in a master-slave replication setup
If we are not talking about master slave setup, then to avoid a coder to write bad code. Establishing a connection to MySQL is very very pricy and should be avoided. I also lazy load the connection, that is the connection is made in the very last minute prior to queries - IF nothing found at cache (both memory and file system caches).

Re: I don't know if this is Singleton but...

Posted: Sat Mar 07, 2009 9:38 pm
by Christopher
kaisellgren wrote:If we are not talking about master slave setup, then to avoid a coder to write bad code. Establishing a connection to MySQL is very very pricy and should be avoided. I also lazy load the connection, that is the connection is made in the very last minute prior to queries - IF nothing found at cache (both memory and file system caches).
Again, lazy loading, reducing connection overhead, etc. are really unrelated to Singleton. And as also noted, your solution is really to use a global, not a Singleton to solve your problem.

Almost always when you use an object you only want one of that object. Singleton solves the problem where you are getting different instances from different sources unpredictably/uncontrollably. That is a specific and rare problem. And there are other solutions to that problem that can be tried before resorting to Singleton.

Re: I don't know if this is Singleton but...

Posted: Sat Mar 07, 2009 10:13 pm
by allspiritseve
kaisellgren wrote:You are not using a singleton design pattern for the database class?
No, I'm not.
kaisellgren wrote:Database is often the slowest node of the chain. Having a singleton effectively makes sure no one will ever establish multinuous connection to the database.
I don't understand why that's something to worry about... If someone's establishing another connection to the database, they're doing so explicitly. A singleton isn't going to protect against that (nor should it, as Josh mentioned it may be necessary to have two different connections.)

Re: I don't know if this is Singleton but...

Posted: Sun Mar 08, 2009 1:00 am
by josh
Yeah besides what if you want to mock out 10 different adapters for testing, or have an adapter that's hitting memcache, etc.. etc... I could think of counter examples all day that are un-related to master slave. The only performance would be the static call which is probably actually faster since its static, youd have to benchmark it. Personally I'd be worried about the optimization of the SQL going over the connection first.

Re: I don't know if this is Singleton but...

Posted: Sun Mar 08, 2009 9:08 am
by kaisellgren
arborint wrote:Again, lazy loading, reducing connection overhead, etc. are really unrelated to Singleton. And as also noted, your solution is really to use a global, not a Singleton to solve your problem.
Yes, I did not say lazy loading and such would be related to Singletons.
arborint wrote:Almost always when you use an object you only want one of that object.
Try telling that to the big world of PHP coders :)

I said it, because the server execution time of a popular shop site I had a look at dropped by roughly 28% after using Singletons. The reason was bad coding within the application and singletons fixed that bad practise. It was not the only way to "fix" the problem, but Singletons were handy.
josh wrote:Personally I'd be worried about the optimization of the SQL going over the connection first.
That one depends so much on the situations. Simple, efficient and small number of queries will not be as bad as multiple connections to the database. Especially if you work with SSL/TSL and remote servers and hundreds of concurrent connection establishments.

One month ago I saw my friend writing a script, which is an Ebay like script and it had this code (something like this):

Code: Select all

$db = new db;
$db2 = new db;
$db3 = new db;
$db4 = new db;
And btw, the constructor initialized the connection to the database. The constructor ate around 95% of the execution time.
allspiritseve wrote:If someone's establishing another connection to the database, they're doing so explicitly.
People explicitly write inefficient and insecure code, I have experienced that way too many times :)

Re: I don't know if this is Singleton but...

Posted: Thu Mar 12, 2009 5:16 am
by = odino =
Well, I started an interesting discussion :P