Details
-
Bug
-
Status: Closed (View Workflow)
-
Minor
-
Resolution: Duplicate
-
3.1.0
-
node@19.6.0
nodemon@2.0.20
mariadb@3.1.0
MariaDB SQL server: 10.10.3
MariaDB SQL server in a docker container. Tested with both node & nodemon. OS: Windows 10
Description
If Node w/mariadb connector & mariadb server losses connection between them, an 'UncaughtException' error will happen. Maybe I'm too new at this, but it seems like there might be an error in the createPool. Desire is to catch the error and let the pool try to reconnect in X time.
Assuming "ClientOptions" are valid and MariaDB server is running, the following will create a pool in Node.js:
const pool = mariadb.createPool(clientOptions)
That is working, but if I simulate that the MariaDB server dies/losing connection (i.e. stops the docker container):
Node will crash after a few seconds with an 'UncaughtException' error.
Seems like createPool is not a promise itself, but it's service is. So not sure how to catch the error and let it try to connect again in X time.
I have tried to listen to events:
- pool.on('error' ... This gives a error message 'ECONNREFUSED'
- process.on('uncaughtException' ... This gives a error message 'ECONNREFUSED'
In both cases Node.js exits, and there seems no possible way to recover from this (beside PM2 or forever; not tested these)
Detailed error message:
node:internal/process/promises:289
triggerUncaughtException(err, true /* fromPromise */);
Error: connect ECONNREFUSED ::1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)
From event:
at D:\Code\node-db-boilerplate\node_modules\mariadb\lib\connection.js:133:13
at new Promise (<anonymous>)
at Connection.connect (D:\Code\node-db-boilerplate\node_modules\mariadb\lib\connection.js:121:12)
at Pool._createConnection (D:\Code\node-db-boilerplate\node_modules\mariadb\lib\pool.js:402:16)
at Pool._doCreateConnection (D:\Code\node-db-boilerplate\node_modules\mariadb\lib\pool.js:40:10)
at listOnTimeout (node:internal/timers:568:17)
at process.processTimers (node:internal/timers:511:7)
Emitted 'error' event on PoolPromise instance at:
at Pool.emit (node:events:512:28)
at D:\Code\node-db-boilerplate\node_modules\mariadb\lib\pool.js:258:22
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)