How to set up a long connection to the YII database

Source: Internet
Author: User
This article mainly introduces about how to set up a long connection of yii database, has a certain reference value, now share to everyone, the need for friends can refer to

1. Set the properties in the configuration file
Add persistent or attributes property in webb/config/main.php

    ' Components ' =>array (        //Database configuration to Mysql         ' dsnmybd ' =>array (            ' class ' = ' cdbconnection ',            ' ConnectionString '    = ' mysql:host=127.0.0.1;port=3306;dbname=my_db ',            ' tableprefix '        = ' pre_ ',            ' Emulateprepare ' +    true,            ' username '            = ' root ',            ' password '            = ' 123456 ',            ' charset '            = ' utf8 ',            ' persistent ' +            true,    //Long connect            //' attributes '        = Array (pdo::attr_persistent = True),    //Long connection          ),          ...      );

2. Detect if long connections are enabled successfully
You can call the Getpersistent () function under Frameworkdbcdbconnection.php, and return true to enable success, false to enable failure.

Example:/web/protected/controllers/testcontroller.php

    Public Function actionlist ()    {        $oQues = Mybd::model ();        Var_dump ($oQues->getpersistent ());    }

Note:

    • If you use a long connection and do not perform any operations on the database for a long time, after the timeout value, the MySQL
      The server shuts down the connection, and the client executes the query with an error similar to "MySQL server has gone away".

    • First log in to MySQL as a superuser, note that you must be a superuser, otherwise you will be prompted not to modify permissions later. And then enter

      Show global variables like ' wait_timeout ';

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.