Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd78c96

Browse files
committed
db changes since mysql loses connectin on heroku
1 parent 744e7a5 commit fd78c96

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

‎db.js‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
var mysql = require('mysql');
22

3-
var connection = mysql.createConnection({
3+
var db_config = {
44
host : '127.0.0.1', // Your host - either local or cloud
55
user : 'root', // your username
66
password : 'root', // your password
77
database : 'your-db-name' // database name
8-
});
9-
connection.connect(function(err) {
10-
if (err) throw err;
11-
});
8+
};
9+
10+
var connection;
11+
12+
function handleDisconnect() {
13+
connection = mysql.createConnection(db_config); // Recreate the connection, since
14+
// the old one cannot be reused.
15+
16+
connection.connect(function(err) { // The server is either down
17+
if(err) { // or restarting (takes a while sometimes).
18+
console.log('error when connecting to db:', err);
19+
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
20+
} // to avoid a hot loop, and to allow our node script to
21+
}); // process asynchronous requests in the meantime.
22+
// If you're also serving http, display a 503 error.
23+
connection.on('error', function(err) {
24+
console.log('db error', err);
25+
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
26+
handleDisconnect(); // lost due to either server restart, or a
27+
} else { // connnection idle timeout (the wait_timeout
28+
throw err; // server variable configures this)
29+
}
30+
});
31+
}
32+
33+
handleDisconnect();
1234

1335
module.exports = connection;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /