4

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 :)

10
  • Why do you want to handle this yourself rather than using the PHP built in persistent connection? Are you doing something special? Commented Dec 31, 2011 at 2:46
  • You can say i'm just stubborn :P ... i want to implement the p conn initial logic (chekc for existing conn) but for a normal conn ... Commented Dec 31, 2011 at 2:56
  • :-) ok. Have you thought about executing SHOW PROCESSLIST against mysql to see what users are connected? Commented Dec 31, 2011 at 2:58
  • yes and i get 2 conn for same user :| thats why i want to check first for the same conn Commented Dec 31, 2011 at 2:59
  • How about SELECT CONNECTION_ID(); Commented Dec 31, 2011 at 3:01

3 Answers 3

0
  • 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
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

It's not for each class dont worry, it's for some of them... i'm just trying to modularise the logic... but i need to keep track of the connections that are the same... ill try to find a way to make available info about conn across the app ... won't be easy , might have to change a lil bit my arhitecture
My bad every class has assigned to it a conn... thought u ment by new conn , different user (some classes have same conn some have different conn but all have a conn assiged to them)... that was my question, i need to check for allready made conn that are the same so it wont bottle up
0

I suggest you read this - I think this will help you http://www.php.net/manual/en/features.persistent-connections.php

1 Comment

yes basically i want to do the thing that persistant connection does...but i dont want a persistant connection...i want to implement its logic for a normal connection... i mean the first thing that the p conn checks for... if that conn exists
0

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());
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.