本文介绍了当Im swtiching从mysql到mysqli驱动程序,我得到太多的连接错误。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么连接太多连接错误:

时切换

  $ db ['default'] ['dbdriver'] ='mysql'; 

TO

  $ db ['default'] ['dbdriver'] ='mysqli'; codeIgniter的db配置文件中的




这表示mysqli驱动程序没有正确关闭所有连接?



strong> UPDATE:



切换数据库驱动程序似乎不是正确的原因(如答案中所示) >

我查看了我的客户端网络托管公司,他们设置max_user_connections为40(没有,不令人印象深刻 - 但我基本上只是想使用group_concat值的用户列表)。

  $ this-> db-> select('u.id AS user_id,u.first_name,
u.housenr,u.address,u.phone,u.garage_nr,u.parking_nr,
u.standing_correction,u.email,u.note,GROUP_CONCAT(wc.consumption)AS消费,
GROUP_CONCAT (wc.id)AS consumption_id,GROUP_CONCAT(wc.consumption_date)AS
consumption_date,GROUP_CONCAT(wc.working_meetings)AS working_meetings,
GROUP_CONCAT(wc.nrof_garage)AS nrof_garage,GROUP_CONCAT(wc.nrof_parking)AS nrof_parking,GROUP_CONCAT(wc.correction)AS correction,,
GROUP_CONCAT(wc.correction_refers)AS correction_refers') - > from('water_consumption wc');
$ this-> db-> join('user u','u.id = wc.user_id');
$ this-> db-> join('role r','u.role_id = r.id');
$ this-> db-> where('r.name','member'); //只有拥有member-role的用户
$ this-> db-> group_by('u.id');
$ this-> db-> order_by('LENGTH(housenr),housenr,first_name'); //自然排序
$ this-> db-> order_by('consumption_date','desc');

$ q = $ this-> db-> get();
if($ q-> num_rows()> 0){
$ res = $ q-> result('Water_consumption');
//更多代码...

这真的很奇怪,在使用时,对每个返回的对象使用一个连接

  $ res = $ q-> result ('Water_consumption'); 

这是我的假设,因为创建了大约40个对象,并设置了max_user_connections

  $ res = $ q- > result(); 

它似乎没有使用那么多的连接和模板显示



这对我的本地wamp没有任何问题,返回所有对象(约60)需要约2-3秒。

解决方案

有时候最明显的答案是盯着你 ...



我说过

  $ res = $ q-> result('Water_consumption'); 

每次都会建立连接。



但是当使用



  $ res = $ q-> result 

没有。



结果是每个 Water_consumption 从另一个类中扩展,该类为处理登录内容创建了一个连接。该类正在建立连接,因此

  $ res = $ q-> result('Water_consumption');包含两个对象的

会建立两个连接。



  $ x = new Water_consumption; 
$ x = new Water_consumption;

这也会创建两个连接。


Why am I getting too many connections error:

when switching from

$db['default']['dbdriver'] = 'mysql';

TO

$db['default']['dbdriver'] = 'mysqli';

in the db config file of codeIgniter??

This indicates that the mysqli driver doesn't close all connections correctly?

UPDATE:

Too many connections appeared when switching db driver didn't seem to be the correct reason (as indicated in the answer)

I've looked at my clients web hosting company and they have set max_user_connections to 40 (no, not impressive - but i basically just want to list of users with values using group_concat).

$this->db->select('u.id AS user_id, u.first_name,
u.housenr, u.address, u.phone, u.garage_nr, u.parking_nr,
u.standing_correction, u.email, u.note, GROUP_CONCAT(wc.consumption) AS consumption,
GROUP_CONCAT(wc.id) AS consumption_id, GROUP_CONCAT(wc.consumption_date) AS
consumption_date, GROUP_CONCAT(wc.working_meetings) AS working_meetings,
GROUP_CONCAT(wc.nrof_garage) AS nrof_garage, GROUP_CONCAT(wc.nrof_parking) AS nrof_parking, GROUP_CONCAT(wc.correction) AS correction, ,
GROUP_CONCAT(wc.correction_refers) AS correction_refers')->from('water_consumption wc');
$this->db->join('user u', 'u.id = wc.user_id');
$this->db->join('role r', 'u.role_id = r.id');
$this->db->where('r.name', 'member'); //Only users with members-role
$this->db->group_by('u.id');
$this->db->order_by('LENGTH(housenr), housenr, first_name'); //Natural sorting
$this->db->order_by('consumption_date', 'desc');

$q = $this->db->get();
if ($q->num_rows() > 0) {
    $res = $q->result('Water_consumption');
    //more code...

This is really strange, but it seems that one connection is used for each returned object when using:

$res = $q->result('Water_consumption');

This is my assumption because about 40 objects are created and max_user_connections is set 40.

BUT when using

$res = $q->result();

it doesn't seem to use that much connections and the template is shown (incorrectly though because it calls function for each Water Consumption object).

This works on my local wamp without any issues and returning all objects (about 60) takes about 2-3 seconds.

解决方案

Sometime the most obvious answers are staring at you...

I stated that

$res = $q->result('Water_consumption');

would make a connection each time.

BUT when using

$res = $q->result();

it didn't.

It turns out that each Water_consumption is extended from another class that makes a connection for handling login stuff. And that class is making a connection, so

$res = $q->result('Water_consumption');

that contained two objects would make two connections.

But the same would happen here..

$x = new Water_consumption;
$x = new Water_consumption;

This would also create two connections.

这篇关于当Im swtiching从mysql到mysqli驱动程序,我得到太多的连接错误。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 11:55