Reputation: 834
I'm creating a web page with select elements which are populated dynamically by AJAX requests.
Upon some event, a select element makes a request to a script which upon execution makes a database connection, queries the database and responds with the data received which will then populate the select element. Each select element makes a separate request at separate events.
In this case, are multiple database connections a bad idea and is there a way to avoid them? I'm using PHP and MySQL.
Upvotes: 3
Views: 1451
Reputation: 7223
Multiple connection don't do any harm. Depending on your webserver and database type it can vary but the default maximum connections is 151 as you can see here. If you exceed this number you will receive a Too many connections
error which means that all available connections are in use by other clients.
The number of connections permitted is controlled by the max_connections system variable. The default value is 151 to improve performance when MySQL is used with the Apache Web server. (Previously, the default was 100.) If you need to support more connections, you should set a larger value for this variable.
If you make a proper connection that fetches and returns the data, this should be done in a few miliseconds, so chances are very small that you will have 151 concurrent open connections that are all working on a query.
So no.. This is not a problem.
Upvotes: 2
Reputation: 10469
If you're only making a few requests per page or per time then this won't be overly expensive. If its a very busy page or site then make a connection pool and share between sessions.
One thing: from your description it sounds like you're letting the page generate SQL on the fly. This sounds like a recipe for an injection attack. Make sure you sanitize the AJAX before you apply it.
Upvotes: 0