reopening mysql connection when closed
This commit is contained in:
parent
9db0c798b0
commit
2d136f0214
@ -22,12 +22,12 @@ export class Server {
|
||||
private initialize(): void {
|
||||
this.app = express();
|
||||
this.httpServer = createServer(this.app);
|
||||
this.mysqlConnection = mysql.createConnection({
|
||||
host: process.env.MYSQL_HOST || "localhost",
|
||||
user: process.env.MYSQL_USER || "root",
|
||||
password: process.env.MYSQL_PASSWORD || "",
|
||||
database: process.env.MYSQL_DATABASE || "hochzeit"
|
||||
});
|
||||
// this.mysqlConnection = mysql.createConnection({
|
||||
// host: process.env.MYSQL_HOST || "localhost",
|
||||
// user: process.env.MYSQL_USER || "root",
|
||||
// password: process.env.MYSQL_PASSWORD || "",
|
||||
// database: process.env.MYSQL_DATABASE || "hochzeit"
|
||||
// });
|
||||
|
||||
this.configureApp();
|
||||
this.configureRoutes();
|
||||
@ -36,8 +36,29 @@ export class Server {
|
||||
|
||||
private async query(sql): Promise<any[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
|
||||
if (!this.mysqlConnection || this.mysqlConnection.state === "disconnected"){
|
||||
console.log("reopening connection...");
|
||||
this.mysqlConnection = mysql.createConnection({
|
||||
host: process.env.MYSQL_HOST || "localhost",
|
||||
user: process.env.MYSQL_USER || "root",
|
||||
password: process.env.MYSQL_PASSWORD || "",
|
||||
database: process.env.MYSQL_DATABASE || "hochzeit"
|
||||
});
|
||||
|
||||
// setTimeout(() => {
|
||||
// this.mysqlConnection.destroy();
|
||||
// }, 5000);
|
||||
}
|
||||
}catch(e){
|
||||
console.error(e);
|
||||
reject(e);
|
||||
}
|
||||
|
||||
this.mysqlConnection.query(sql, (error, results) => {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
reject(error)
|
||||
} else {
|
||||
resolve(results);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user