Reconnect logic on connection close in X DevAPI of MySql using @mysql/xdevapi module in Node.js
I am using X DevAPI of MySql v8.0.33 using @mysql/xdevapi v8.0.33.
I have reconnect logic added but that is not working. Here is that code.
const mysqlx = require('@mysql/xdevapi')
async function createSession() {
try {
const session = await mysqlx.getSession({
user: 'your_user',
password: 'your_password',
host: 'your_host',
port: 33060, // default MySQL X Protocol port
});
// Handle connection close by reconnecting the session
session['_client']['_stream'].on('close', () => {
console.log('Connection closed. Reconnecting...');
createSession(); // Re-establish the session
});
// Use the session for your database operations
// ...
} catch (error) {
console.error('Error connecting to MySQL:', error);
}
}
// Create the initial session
createSession();
I am getting error
TypeError: Cannot read properties of undefined (reading '_stream')
This error means that session['_client'] is undefined.
What is wrong with my reconnect logic?
Subject
Written By
Posted
Reconnect logic on connection close in X DevAPI of MySql using @mysql/xdevapi module in Node.js
May 30, 2023 08:31AM
September 04, 2023 03:55AM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.