2

I have a Joomla1.5 based system. I run the profiler and found that mysql_connect() and mysql_close() both functions have been used 16 times. I know that both of these functions and Joomla 1.5 are deprecated and strongly prohibited by the community. My system is facing performance issue and sometimes during peak hours DB connection threads become too high (>100) and system running slower. While we have a dedicated Windows server.

Below is my DB server configuration:
Windows edition: Windows Server 2008 R2 Standard Service Pack 1
Processor: Intel(R) Xeon(R) CPU X5460 @3.16GHZ 3.16 GHZ (2 Processors)
Installed Memory(RAM): 8.00 GB
System type: 64-Bit Operating system

mysql_connect() function has been used in the constructor of JDatabaseMySQL class. It means whenever we create database object, this mysql_connect() is getting executed and create a new connection.

  • I am curious to know whether calling mysql_connect() function multiple times creating such high DB connection threads issue?
  • Is it possible to use a single connection instead of initiating objects so many times? If yes, how can I do that?
  • Will Replacement of mysql_connection() by mysqli_connect() help us in the improvement of system performance?
  • How Joomla manages the connection pool?

Please provide your answer without suggesting upgrading Joomla.

ursitesion
  • 988
  • 2
  • 15
  • 26

1 Answers1

3

Joomla! itself only connects to the database a single time. It uses the singleton pattern to either get the existing database connection or create it if needed.

It sounds like you have some custom code or a Joomla! extension is instantiating the JDatabase class multiple times. You shouldn't instantiate or deal with the class directly, instead you should use the JFactory to obtain the database object, which internally will either get the existing or create new if needed:

$db = JFactory::getDBO();
MrCode
  • 63,975
  • 10
  • 90
  • 112
  • Can you please describe how Joomla manages the connection pool? How will it check the existing DB connection? – ursitesion Mar 19 '14 at 10:58
  • 1
    The connection is managed by `JFactory`. When some code needs the database, `JFactory` checks if it already has a connection object and if yes, return it, or if not then create a new connection and return it. Obviously this only works when all code is using `JFactory::getDBO()`. If some custom code is instantiating JDatabase directly then it will always create a new connection. – MrCode Mar 19 '14 at 11:07
  • Also, Joomla! always creates a new database connection with each request, it doesn't share a connection across multiple requests. Also consider if ajax or other requests contribute to what your profiler is watching. – MrCode Mar 19 '14 at 11:10
  • Thanks @MrCode, I am unable to understand your comment - `Joomla! always creates a new database connection with each request, it doesn't share a connection across multiple requests.` It is leading to some confusion when read your first comment. Can you please clarify it? – ursitesion Mar 19 '14 at 12:51
  • To clarify, Joomla! creates a new database connection for each HTTP request to a page, that connection is then shared to any code that executes during the request, so 1 connection for 1 HTTP request. Some requests will not initiate the Joomla! core and so won't create a connection, for example images and CSS/Javascript. – MrCode Mar 19 '14 at 13:01
  • Many Thanks @MrCode. Can you also broaden your answers by responding my other points? – ursitesion Mar 19 '14 at 16:53
  • To create database object, I have used everywhere in my custom extensions - `$db = & JFactory::getDBO();` – ursitesion Mar 20 '14 at 12:39
  • Yes that is the correct way so you will either get a new connection or the existing connection. – MrCode Mar 20 '14 at 17:23