问题
I have a class for each database table object. Each class takes care of connection/queries/data format (database table specific logic) as I prefer to assign a connection for each class for better modularity. Also, I am using specific connections for some tables and queries.
My question is how can I check if a connection already exists so it won't start another one?
Basically I want to check if the connection with the same username/password/database has already been made. Or is this not necessary because mySql won't start a new connection for the same user? If that is so then please explain.
After further research found out that this is not possible ... it would require to get the thread id and use the conn with the thread id and it would act like a persistant conn; Or it would require to keep track every thread ids used during a script and it's kinda useless.
There isn't yet a spimple method of doing this ... persistant connection can be a choice but then u have to take care of connection clean up and again sux :)
P.S. : Eventually i did made a tracking system for the connections during my app run (the guys that said to store them in a globally available object we're right). Stored conn object and conn params in 2 arrays in a singleton class object, checked if the params allready exists in params array , if not make new conn , if yes get the conn object from the array-key thats the same with the key where params were found... I allready had the arhitecture made for this but didnt want to use that class...not the logic i began with :), and also wanted to make it in a library item, thats why i was looking for a simple and abstract solution but there is only a particular solution :)
回答1:
- Create new connection for each class is not a good idea. It may be modularized to you but your mysql server will be soon bloated with
too may connections
error.
I suggest use singleton pattern and some OO.
class Singleton{
private static $instance=null;
public function connection(){
if(self::$instance==null){
self::$instance = mysql_connect(); // define it in your way,
}
return self::$connection;
}
}
class TableA extends Singleton{
function find($id){
$query="select * from `A` where `id`='$id'";
mysql_query($query, $this->connection());
... // other codes
}
}
回答2:
I suggest you read this - I think this will help you http://www.php.net/manual/en/features.persistent-connections.php
回答3:
If you must have a different connection for each class, you can use a static class property to hold the connection, and use the singleton pattern to retrieve it.
class SomeTable {
// Connection resource as a static property
// Since it is static, it will be shared by all instances of class SomeTable
public static $db = FALSE;
// Static method to retrieve the connection
// or create it if it doesn't exist
public static function get_conn() {
if (self::$db) {
// Just return the connection if it already exists
return self::$db;
}
else {
// If it doesn't already exist, create it and return it
self::$db = mysql_connect(...);
return self::$db;
}
}
// In other methods, use self::get_conn()
public function someQuery() {
$sql = "SELECT col FROM tbl";
// Call the connection singleton explicitly
// as the second param to mysql_query()
$result = mysql_query($sql, self::get_conn());
}
}
来源:https://stackoverflow.com/questions/8685896/check-if-a-specific-mysql-connection-already-exists-during-a-php-script