diff --git a/lib/mysql.js b/lib/mysql.js index 852201e..4a700e6 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -12,26 +12,17 @@ const pool = mysql.createPool({ queueLimit: 0 }); -// Variable to store interval ID let keepAliveInterval = null; -// Event listener for monitoring pool pool.on('connection', function (connection) { - console.log('New MySQL connection established as id ' + connection.threadId); - - // Set session timeout for this connection connection.query("SET SESSION wait_timeout = 28800"); connection.query("SET SESSION interactive_timeout = 28800"); }); pool.on('error', function(err) { - console.log('MySQL Pool Error:', err); - if(err.code === 'PROTOCOL_CONNECTION_LOST') { - console.log('Connection lost, pool will handle reconnection automatically'); - } + console.error('MySQL Pool Error:', err); }); -// Function to retry query with exponential backoff const retryQuery = async (queryStr, bindings = [], maxRetries = 3, delay = 1000) => { for (let attempt = 1; attempt <= maxRetries; attempt++) { try { @@ -44,7 +35,6 @@ const retryQuery = async (queryStr, bindings = [], maxRetries = 3, delay = 1000) }); }); } catch (err) { - // Only retry for connection-related errors const isConnectionError = ( err.code === 'PROTOCOL_CONNECTION_LOST' || err.code === 'ECONNRESET' || @@ -52,19 +42,16 @@ const retryQuery = async (queryStr, bindings = [], maxRetries = 3, delay = 1000) err.code === 'ENOTFOUND' || err.code === 'ECONNREFUSED' || err.code === 4031 || - err.errno === 2013 || // Lost connection to MySQL server during query - err.errno === 2006 // MySQL server has gone away + err.errno === 2013 || + err.errno === 2006 ); - // If not a connection error or already reached max retry, throw immediately if (!isConnectionError || attempt >= maxRetries) { throw err; } - console.log(`Query attempt ${attempt} failed (connection error):`, err.message); - console.log(`Retrying in ${delay}ms... (attempt ${attempt + 1}/${maxRetries})`); await new Promise(resolve => setTimeout(resolve, delay)); - delay *= 2; // Exponential backoff + delay *= 2; } } }; @@ -73,7 +60,6 @@ const query = async (queryStr, bindings = []) => { try { return await retryQuery(queryStr, bindings); } catch (err) { - // Only log error if this is a connection error that has been retried const isConnectionError = ( err.code === 'PROTOCOL_CONNECTION_LOST' || err.code === 'ECONNRESET' || @@ -85,44 +71,36 @@ const query = async (queryStr, bindings = []) => { err.errno === 2006 ); - // Only log if connection error (since it has been retried) if (isConnectionError) { - console.log('Final query error after retries:', err); + console.error('Final query error after retries:', err); } throw err; } }; -// Function to test connection const testConnection = async () => { try { await query('SELECT 1'); - console.log('Database connection test successful'); return true; } catch (err) { - console.log('Database connection test failed:', err.message); + console.error('Database connection test failed:', err.message); return false; } }; -// Function to start keep-alive const startKeepAlive = () => { if (keepAliveInterval) { - console.log('Keep-alive already running'); return; } keepAliveInterval = setInterval(async () => { try { await query('SELECT 1'); - console.log('Keep-alive query successful'); } catch (err) { - console.log('Keep-alive query failed:', err.message); + console.error('Keep-alive query failed:', err.message); } - }, 300000); // Every 5 minutes - - console.log('Keep-alive started (every 5 minutes)'); + }, 300000); }; // Function to stop keep-alive @@ -130,26 +108,19 @@ const stopKeepAlive = () => { if (keepAliveInterval) { clearInterval(keepAliveInterval); keepAliveInterval = null; - console.log('Keep-alive stopped'); - } else { - console.log('Keep-alive is not running'); } }; // Function to close pool with graceful shutdown const closePool = () => { return new Promise((resolve) => { - // Stop keep-alive first stopKeepAlive(); - pool.end(() => { - console.log('MySQL pool closed'); resolve(); }); }); }; -// Function for comprehensive health check const healthCheck = async () => { const health = { status: 'unknown', @@ -180,7 +151,6 @@ const healthCheck = async () => { health.database.responseTime = responseTime; health.status = 'healthy'; - // Additional check for response time if (responseTime > 5000) { health.status = 'slow'; } else if (responseTime > 1000) { @@ -191,8 +161,7 @@ const healthCheck = async () => { health.database.connected = false; health.database.error = err.message; health.status = 'unhealthy'; - - console.log('Health check failed:', err.message); + console.error('Health check failed:', err.message); } return health; @@ -204,7 +173,7 @@ const isHealthy = async () => { await query('SELECT 1'); return true; } catch (err) { - console.log('Health check failed:', err.message); + console.error('Health check failed:', err.message); return false; } }; diff --git a/package-lock.json b/package-lock.json index 0f4339f..ef37b44 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "letsql", - "version": "1.2.1", + "version": "1.2.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "letsql", - "version": "1.2.1", + "version": "1.2.2", "license": "MIT", "dependencies": { "dotenv": "^16.4.5", @@ -2933,9 +2933,9 @@ "license": "MIT" }, "node_modules/mysql2": { - "version": "3.14.4", - "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.14.4.tgz", - "integrity": "sha512-Cs/jx3WZPNrYHVz+Iunp9ziahaG5uFMvD2R8Zlmc194AqXNxt9HBNu7ZsPYrUtmJsF0egETCWIdMIYAwOGjL1w==", + "version": "3.14.5", + "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.14.5.tgz", + "integrity": "sha512-40hDf8LPUsuuJ2hFq+UgOuPwt2IFLIRDvMv6ez9hKbXeYuZPxDDwiJW7KdknvOsQqKznaKczOT1kELgFkhDvFg==", "license": "MIT", "dependencies": { "aws-ssl-profiles": "^1.1.1", diff --git a/package.json b/package.json index 7c23052..8c68e67 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "letsql", - "version": "1.2.1", + "version": "1.2.2", "description": "A lightweight and user-friendly Node.js ORM module for MySQL databases. Inspired by Eloquent in Laravel.", "main": "index.js", "scripts": {