diff --git a/README.md b/README.md index 2c43809678..7f7e8fed43 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,9 @@ const pool = mysql.createPool({ connectionLimit: 10, maxIdle: 10, // max idle connections, the default value is the same as `connectionLimit` idleTimeout: 60000, // idle connections timeout, in milliseconds, the default value 60000 - queueLimit: 0 + queueLimit: 0, + enableKeepAlive: true, + keepAliveInitialDelay: 0 }); ``` The pool does not create all connections upfront but creates them on demand until the connection limit is reached. diff --git a/documentation_zh-cn/README.md b/documentation_zh-cn/README.md index 9e33860d42..db420eb84f 100644 --- a/documentation_zh-cn/README.md +++ b/documentation_zh-cn/README.md @@ -132,7 +132,9 @@ const pool = mysql.createPool({ database: 'test', waitForConnections: true, connectionLimit: 10, - queueLimit: 0 + queueLimit: 0, + enableKeepAlive: true, + keepAliveInitialDelay: 0 }); ``` 该池不会预先创建所有连接,而是根据需要创建它们,直到达到连接限制。 diff --git a/lib/connection.js b/lib/connection.js index ed8442aac9..8e861bca8a 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -52,9 +52,10 @@ class Connection extends EventEmitter { opts.config.host ); - // Enable keep-alive on the socket. It's disabled by default, but the - // user can enable it and supply an initial delay. - this.stream.setKeepAlive(true, this.config.keepAliveInitialDelay); + // Optionally enable keep-alive on the socket. + if (this.config.enableKeepAlive) { + this.stream.setKeepAlive(true, this.config.keepAliveInitialDelay); + } // Enable TCP_NODELAY flag. This is needed so that the network packets // are sent immediately to the server diff --git a/lib/connection_config.js b/lib/connection_config.js index edb7fcff90..236cf6747f 100644 --- a/lib/connection_config.js +++ b/lib/connection_config.js @@ -115,7 +115,7 @@ class ConnectionConfig { this.debug = options.debug; this.trace = options.trace !== false; this.stringifyObjects = options.stringifyObjects || false; - this.enableKeepAlive = !!options.enableKeepAlive; + this.enableKeepAlive = options.enableKeepAlive !== false; this.keepAliveInitialDelay = options.keepAliveInitialDelay || 0; if ( options.timezone && diff --git a/typings/mysql/lib/Pool.d.ts b/typings/mysql/lib/Pool.d.ts index 1bace502db..4ad74e772f 100644 --- a/typings/mysql/lib/Pool.d.ts +++ b/typings/mysql/lib/Pool.d.ts @@ -44,13 +44,12 @@ declare namespace Pool { queueLimit?: number; /** - * Enable keep-alive on the socket. It's disabled by default, but the - * user can enable it and supply an initial delay. + * Enable keep-alive on the socket. (Default: true) */ enableKeepAlive?: boolean; /** - * If keep-alive is enabled users can supply an initial delay. + * If keep-alive is enabled users can supply an initial delay. (Default: 0) */ keepAliveInitialDelay?: number; }