Reputation: 21531
At the moment I load the database class in my autoload which automatically creates a connection to mysql. If there are no queries it will have still created a connection.
For performance I only want to connect to mysql if a query has been run.
What would be the best way to achieve this?
I am thinking of writing a model function that all queries run through which detects if the database has been connected to or not, and simply calls $this->load->database()
if not before running $this->db->query()
.
The problem with this is that I would have to change all of my $this->db->query()
references in my code which is a pain.
Ideally I would like to extend the $this->db->query()
function to support this.
Any ideas?
Upvotes: 2
Views: 1306
Reputation: 12037
You can modify the autoinit property of your database config
$db['mydb']['autoinit'] = false;
This will cause you database class to not initialize (which include connecting to the server) when instantiated, it will instead happen when the first query occurs.
See the database configuration page
Upvotes: 3