CodeFoodPixels / node-promise-mysql

A wrapper for mysqljs/mysql that wraps function calls with Bluebird promises.
MIT License
338 stars 64 forks source link

Connection lost The server closed the connection #166

Closed burhanahmeed closed 1 year ago

burhanahmeed commented 1 year ago

This happen quite sometimes, how do I reconnect automatically when the connection lost?

I am using createPool

const connection = await mysql
            .createPool({
                user: DB_USER || dbUser,
                password: DB_PASS || dbPass, 
                database: DB_DATABASE || dbDatabase,
                host: DB_HOST || dbHost,
                port: DB_PORT || 3306,
                ...config,
            })
            .catch((err) => {
                throw customError(err);
            });

and pass the connection to helper function to execute the query

export const executeSQL = async (sql: string, type: string, conn: Pool): Promise<any> => {
    try {
        const results = await conn.query(sql);

               return results;
    } catch (error) {
        console.log(error);

        throw error;
    }
};
CodeFoodPixels commented 1 year ago

Use the reconnect option mentioned in the readme: https://github.com/CodeFoodPixels/node-promise-mysql#connectionoptions-object