-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqlConfig.js
51 lines (42 loc) · 1.18 KB
/
mysqlConfig.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
const mysql = require('mysql')
const login_info = {
host: '127.0.0.1',
user: 'root',
password: 'password',
database: 'test',
multipleStatements: true,
}
const MYSQL_POOL = mysql.createPool(login_info)
function handleDisconnect(pool, conn) {
conn.destroy()
let intervalId = setInterval(
() =>
pool.getConnection((err, connection) => {
if (err) {
console.log(new Date(), 'Error when connecting to db :', err.code)
} else {
conn = connection
conn.release()
clearInterval(intervalId)
}
}),
2000
)
}
MYSQL_POOL.on('connection', (connection) => {
console.log(new Date(), "MySQL Connection created")
connection.on('error', (err) => {
handleDisconnect(MYSQL_POOL, connection)
console.error(new Date(), 'MySQL Connection error', err.code)
})
connection.on('close', (err) =>
console.error(new Date(), 'MySQL Connection close', err)
)
})
cleanUpServer = (e) => MYSQL_POOL.end(() => process.exit(0))
[`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `uncaughtException`, `SIGTERM`].forEach((eventType) => {
process.on(eventType, cleanUpServer.bind(eventType))
})
module.exports = {
MYSQL_POOL
}