检查特定的mysql连接是否已经存在于php脚本中? [英] Check if a specific mysql connection already exists during a php script?

查看:105
本文介绍了检查特定的mysql连接是否已经存在于php脚本中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为每个数据库表对象都有一个类.每个类都负责连接/查询/数据格式(数据库表特定的逻辑),因为我更喜欢为每个类分配一个连接,以实现更好的模块化.另外,我正在对某些表和查询使用特定的连接.

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?

基本上我想检查是否已经建立了具有相同用户名/密码/数据库的连接.还是因为MySql不会为同一用户启动新连接而这样做是没有必要的?如果是这样,请解释.

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.

经过进一步研究后发现这是不可能的……它将需要获取线程ID并使用带有该线程ID的conn,它的作用类似于持久性conn.否则它将需要跟踪脚本中使用的每个线程ID,这有点用.

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.

还没有一种简便的方法...持久连接可以选择,但是随后您必须清理连接,然后再次进行sux:)

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. :最终,我在应用运行期间确实为连接建立了跟踪系统(说过将它们存储在全球可用的对象中的人是正确的).将conn对象和conn参数存储在一个单例类对象的2个数组中,检查params数组中是否已经存在所有params,如果不是,则创建新的conn,如果是,则从array-key中获取conn对象,该键与params所在的键相同被发现...我已经准备好了这个结构,但不想使用该类...不是我开始的逻辑:),还想在一个库中创建它,这就是为什么我要寻找一个简单抽象的解决方案,但只有一个特定的解决方案:)

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

推荐答案

  • 为每个类创建新的连接不是一个好主意.它可能已模块化,但您的mysql服务器很快会出现too may connections错误.
    • 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.
    • 我建议使用单例模式和一些OO.

      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
          }
      }
      

      这篇关于检查特定的mysql连接是否已经存在于php脚本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆