Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The server to receive data after a number of database connections will become more, eventually leading to error. #187

Closed
cnsms opened this issue Jul 5, 2017 · 5 comments
Labels

Comments

@cnsms
Copy link

cnsms commented Jul 5, 2017

6 clients send data once per minute. About twenty and twenty minutes after the server is being given.

1
2
3

@walkor
Copy link
Owner

walkor commented Jul 5, 2017

Creating mysql connections without closing them can cause this too many connections error.
So the simple way is just close all mysql connection at the end of onMessage.
The best way is to create a global mysql connection and all requests reuse this connection. It looks like this.

$worker = new Worker(...);
// Global db connection.
$db_connection = null;
$worker->onWorkerStart = function($worker) {
    // Create global db connection.
    global $db_connection;
    $db_connection = new your\db\Connection(....);
};
$worker->onMessage = function($client_connection, $data) {
    // Reuse global db connection.
    global $db_connection;
    $db->connection->query(....);
};

@cnsms
Copy link
Author

cnsms commented Jul 5, 2017

ok,i'll try it.

@walkor walkor closed this as completed Jul 5, 2017
@cnsms
Copy link
Author

cnsms commented Jul 5, 2017

Is not the database can not use tp5?
You write this is a separate re-connected to the database slightly.

@taozywu
Copy link

taozywu commented Jul 5, 2017

@cnsms

$worker = new Worker(...);
// Global db connection.
$db_connection = null;
$db_configs = [
     "host" => "localhost",
     "user" => "root",
     "dbtable" => "test",
     "password" => "123456!@#",
     "charset" => "",
];
$worker->onWorkerStart = function($worker) {
    // Create global db connection.
    global $db_connection, $db_configs;
    require_once your_path . "/think/db/connector/mysql.php";
    // $db_connection = new \think\db\connector\MySQL($db_configs);
    $db_connection = Db::connect($db_configs);
};

$worker->onMessage = function($client_connection, $data) {
    // Reuse global db connection.
    global $db_connection ;
    $result = $db_connection ->table('')->select();
    file_put_contents("/tmp/tmp.log", var_export($result, true)."\n", FILE_APPEND));
};

--
like this!

@cnsms
Copy link
Author

cnsms commented Jul 5, 2017

@taozywu ok,i'll try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants