-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn.js
70 lines (59 loc) · 2.14 KB
/
conn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const mysql = require('mysql');
const db_config = {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME
};
var con = mysql.createPool(db_config);
// Attempt to catch disconnects
con.on('connection', function (connection) {
console.log('DB Connection established');
connection.on('error', function (err) {
console.error(new Date(), 'MySQL error', err.code);
});
connection.on('close', function (err) {
console.error(new Date(), 'MySQL close', err);
});
});
module.exports = con;
// function handleDisconnect() {
// console.log("\n New connection tentative...");
// con = mysql.createConnection(db_config);
// //- Destroy the current connection variable
// //- Try to reconnect
// con.connect(function (err){
// if(err) {
// console.log('error when connecting to db:', err);
// setTimeout(handleDisconnect(), 2000);
// }
// else {
// console.log("\n\t *** New connection established with the database. ***")
// }
// });
// con.on('error', function(err) {
// console.log('db error', err);
// if(err.code === 'PROTOCOL_CONNECTION_LOST') {
// handleDisconnect(con);
// } else if(err.code === "PROTOCOL_ENQUEUE_AFTER_QUIT"){
// console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
// handleDisconnect(con);
// }
// //- Fatal error : connection variable must be recreated
// else if(err.code === "PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR"){
// console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
// handleDisconnect();
// }
// //- Error because a connection is already being established
// else if(err.code === "PROTOCOL_ENQUEUE_HANDSHAKE_TWICE"){
// console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
// }
// //- Anything else
// else{
// console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
// handleDisconnect();
// }
// });
// }
// handleDisconnect();
// module.exports = con;