MySQL新增一个连接源码

 当客户端向服务器发起查询时,就是和服务器之间建立了一个连接。而MySQL是提供了一个最大连接数限制的。所以,每次在一个连接建立成功后,服务器要给该连接分配处理线程的时候会判断现在的连接数是否已经操作了配置的最大连接数了。如果已经超过,则不会再分配线程来处理,直接关闭在连接。

 

static void create_new_thread(THD *thd)

{

  DBUG_ENTER("create_new_thread");

 

  /*

    Don't allow too many connections. We roughly check here that we allow

    only (max_connections + 1) connections.

  */

 

  mysql_mutex_lock(&LOCK_connection_count);

 

  if (connection_count >= max_connections + 1 || abort_loop)

  {//判断是否超过最大连接或是否标志终止,该最大连接是在my.ini文件中配置的,

 

    mysql_mutex_unlock(&LOCK_connection_count);

 

    DBUG_PRINT("error",("Too many connections"));

    close_connection(thd, ER_CON_COUNT_ERROR);

    delete thd;

    DBUG_VOID_RETURN;

  }

 

  ++connection_count;//如果没有,增加连接数

 

  if (connection_count > max_used_connections)

    max_used_connections= connection_count;//增加最大使用连接数

 

  mysql_mutex_unlock(&LOCK_connection_count);

 

  /* Start a new thread to handle connection. */

 

  mysql_mutex_lock(&LOCK_thread_count);

 

  /*

    The initialization of thread_id is done in create_embedded_thd() for

    the embedded library.

    TODO: refactor this to avoid code duplication there

  */

  thd->thread_id= thd->variables.pseudo_thread_id= thread_id++;

 

  thread_count++;//增加线程数,准备为该连接分配线程了(见面的函数)

 

  MYSQL_CALLBACK(thread_scheduler, add_connection, (thd));

 

  DBUG_VOID_RETURN;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值