简体   繁体   English

检查php脚本中是否已存在特定的mysql连接?

[英]Check if a specific mysql connection already exists during a php script?

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? 或者这不是必需的,因为mySql不会为同一个用户启动新连接? 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; 经过进一步的研究发现这是不可能的......它需要获取线程id并使用带有线程id的conn,它将像一个持久的conn; Or it would require to keep track every thread ids used during a script and it's kinda useless. 或者它需要跟踪脚本中使用的每个线程ID,这有点无用。

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 :) 还没有一个spimple方法这样做...持久连接可以是一个选择但是你必须照顾连接清理和再次sux :)

PS : 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). PS:最终我确实在我的应用程序运行期间为连接创建了一个跟踪系统(那些说它们存储在全局可用对象中的人我们是对的)。 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 :) 在单例类对象中的2个数组中存储conn对象和conn params,检查params是否已经存在于params数组中,如果不是则创建新的conn,如果是,则从数组键获取conn对象与params相同被发现......我已经为此做了一个架构但是不想使用那个类...不是我开始的逻辑:),而且还想把它放在一个库项目中,这就是为什么我在寻找一个简单而抽象的解决方案,但只有一个特定的解决方案:)

我建议你读这个 - 我想这会对你有帮助http://www.php.net/manual/en/features.persistent-connections.php

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());
  }
}
  • 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. 它可能是模块化的,但你的mysql服务器很快就会膨胀, too may connections错误。

I suggest use singleton pattern and some OO. 我建议使用单例模式和一些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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM