Skip to content

Commit 057425e

Browse files
committed
docs: remove handleDisconnect example
This removes the code example that tries to create a new connection after a connection is disconnected. This example came about before there was a `Pool` in this library and was necessary. Now this example is simply mis-leading and users end up trying to copy-and-paste it into a stand-alone file and get confused on how to replace the `connection` variable reference. `Pool` is the official solution.
1 parent 3bb9757 commit 057425e

File tree

1 file changed

+2
-39
lines changed

1 file changed

+2
-39
lines changed

Readme.md

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -382,45 +382,8 @@ events are considered fatal errors, and will have the `err.code =
382382
'PROTOCOL_CONNECTION_LOST'`. See the [Error Handling](#error-handling) section
383383
for more information.
384384

385-
A good way to handle such unexpected disconnects is shown below:
386-
387-
```js
388-
var db_config = {
389-
host: 'localhost',
390-
user: 'root',
391-
password: '',
392-
database: 'example'
393-
};
394-
395-
var connection;
396-
397-
function handleDisconnect() {
398-
connection = mysql.createConnection(db_config); // Recreate the connection, since
399-
// the old one cannot be reused.
400-
401-
connection.connect(function(err) { // The server is either down
402-
if(err) { // or restarting (takes a while sometimes).
403-
console.log('error when connecting to db:', err);
404-
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
405-
} // to avoid a hot loop, and to allow our node script to
406-
}); // process asynchronous requests in the meantime.
407-
// If you're also serving http, display a 503 error.
408-
connection.on('error', function(err) {
409-
console.log('db error', err);
410-
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
411-
handleDisconnect(); // lost due to either server restart, or a
412-
} else { // connnection idle timeout (the wait_timeout
413-
throw err; // server variable configures this)
414-
}
415-
});
416-
}
417-
418-
handleDisconnect();
419-
```
420-
421-
As you can see in the example above, re-connecting a connection is done by
422-
establishing a new connection. Once terminated, an existing connection object
423-
cannot be re-connected by design.
385+
Re-connecting a connection is done by establishing a new connection. Once
386+
terminated, an existing connection object cannot be re-connected by design.
424387

425388
With Pool, disconnected connections will be removed from the pool freeing up
426389
space for a new connection to be created on the next getConnection call.

0 commit comments

Comments
 (0)