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).josh wrote: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 setupkaisellgren wrote:Having a singleton effectively makes sure no one will ever establish multinuous connection to the database.
I don't know if this is Singleton but...
Moderator: General Moderators
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: I don't know if this is Singleton but...
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: I don't know if this is Singleton but...
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.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).
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.
(#10850)
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: I don't know if this is Singleton but...
No, I'm not.kaisellgren wrote:You are not using a singleton design pattern for the database class?
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.)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.
Re: I don't know if this is Singleton but...
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.
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: I don't know if this is Singleton but...
Yes, I did not say lazy loading and such would be related to Singletons.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.
Try telling that to the big world of PHP codersarborint wrote:Almost always when you use an object you only want one of that object.
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.
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.josh wrote:Personally I'd be worried about the optimization of the SQL going over the connection first.
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;People explicitly write inefficient and insecure code, I have experienced that way too many timesallspiritseve wrote:If someone's establishing another connection to the database, they're doing so explicitly.
Re: I don't know if this is Singleton but...
Well, I started an interesting discussion 