I have a class for each database table object. Each class takes care of the connection / query / data format (specific database logic), since I prefer to assign a connection to each class for better modularity. In addition, I use certain joins for some tables and queries.
My question is: how can I check if a connection exists, so it wonβt start another?
Basically, I want to check if a connection has already been made with the same username / password / database. Or is it not necessary because mySql does not start a new connection for the same user? If so, please explain.
After further research, it turned out that this is impossible ... for this you will need to get the stream identifier and use conn with the stream identifier, and it will act as a stable contact; Or you would need to keep track of all the thread IDs used during the script, and this is useless.
There is still no easy way to do this ... a persistent connection may be a choice, but then you need to take care of the connection by clearing and sux again :)
PS: In the end, I made a tracking system for connections during my launch of the application (the guys who said to save them in a globally accessible object, we are right). Stored conn object and conn params in 2 arrays in a single-element class object, it is checked whether there are params allready in the params array, if you do not create a new conn, if so, get the conn object from the key array, the same with the key where params were found ... I already had an architecture made for this, but didnβt want to use this class ... not the logic that I started with :), but also wanted to do it in a library element, which is why I was looking for a simple and abstract solution but there is only a specific solution :)
Tudor
source share