Hello!
Current Model logic creates a db connection as soon as any Model is defined, because of the __constructor of Model.php.
This is a pretty standard behavior for a light app but may create some problems when you're dealing with a lot of requests. Let me elaborate on that:
To handle a lot of incoming "get"/"read" request we implemented a very strong caching policy inside all of our SomethingModel -> getWhatever and we go to the DB only when needed.
Everything is working perfectly performance-wise except when we're having a spike of incoming calls because those were being translated to "empty" (read without any query) connections to MySQL - the only query that was passing is the infamous query to set the STRICT_ALL_TABLES.
That is pretty logic because that is generated by Database::connect() that is somehow warming up the connection someone will later use.
The problem is that in our scenario it greatly affect performances because we have to configure MySQL to at least support thousands of (empty) connections, and, since MySQL is on another machine, it also slows down the application because it's somehow connecting and authenticating to something else on the network.
I have seen that, in general, the query() stuff checks for a valid connection and if there isn't any, it will trigger a Database::connect() itself, so I was wondering how hard it would be do implement a Lazy DB Connection that does nothing when the Model is created and postpone the real connection to the moment any real DB activity is required.
If you think it's reasonable (and maybe have some hints on how to cleanly do that) I can also give it a shot and send a pull request - just wanted to make sure everything makes sense.
Thanks.
Current Model logic creates a db connection as soon as any Model is defined, because of the __constructor of Model.php.
This is a pretty standard behavior for a light app but may create some problems when you're dealing with a lot of requests. Let me elaborate on that:
To handle a lot of incoming "get"/"read" request we implemented a very strong caching policy inside all of our SomethingModel -> getWhatever and we go to the DB only when needed.
Everything is working perfectly performance-wise except when we're having a spike of incoming calls because those were being translated to "empty" (read without any query) connections to MySQL - the only query that was passing is the infamous query to set the STRICT_ALL_TABLES.
That is pretty logic because that is generated by Database::connect() that is somehow warming up the connection someone will later use.
The problem is that in our scenario it greatly affect performances because we have to configure MySQL to at least support thousands of (empty) connections, and, since MySQL is on another machine, it also slows down the application because it's somehow connecting and authenticating to something else on the network.
I have seen that, in general, the query() stuff checks for a valid connection and if there isn't any, it will trigger a Database::connect() itself, so I was wondering how hard it would be do implement a Lazy DB Connection that does nothing when the Model is created and postpone the real connection to the moment any real DB activity is required.
If you think it's reasonable (and maybe have some hints on how to cleanly do that) I can also give it a shot and send a pull request - just wanted to make sure everything makes sense.
Thanks.
