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!