Lifecycle and Shutdown
Last updated
This page covers the full pool lifecycle, from creation in
init() to shutdown through close().init()
- Resolves the read and write configs by priority (see Configuration)
- Creates a pool with
createPool()for each side whosedatabaseis non-empty - Checks out one connection from each pool and releases it to verify credentials and network
- On any failure, calls
console.error("MySQL initialization failed:", err)and rethrows the original error
close()
Calls
end() on the read pool and then the write pool and sets both references to null (src/MySQLPool.ts:99-115). Later queries throw ... connection is not available., and you can call init() again to recreate the pools. On failure it prints Failed to close MySQL connections: and rethrows the original error.- import MySQLPool from "@pardnchiu/mysql-pool";
- await MySQLPool.init();
- try {
- await MySQLPool.table("jobs", "write").where("status", "pending").get();
- } finally {
- await MySQLPool.close();
- }
Signal Handlers
Importing the package registers
SIGINT and SIGTERM listeners at module load (src/MySQLPool.ts:409-417). On either signal it awaits close() and then calls process.exit(0), so Ctrl+C or a container stop releases both pools without extra code.| Item | Behavior |
|---|---|
| Registered | Once, when the module is first loaded |
| Action | await MySQLPool.close() → process.exit(0) |
| Exit code | Always 0 |
| Your own listeners | Run alongside it; the package's listener exits the process once close() finishes, so finish your own cleanup before that |
States
stateDiagram-v2
[*] --> Uninitialized
Uninitialized --> Ready: init() succeeds
Uninitialized --> Uninitialized: init() fails and throws
Ready --> Closed: close()
Closed --> Ready: init()- stateDiagram-v2
- [*] --> Uninitialized
- Uninitialized --> Ready: init() succeeds
- Uninitialized --> Uninitialized: init() fails and throws
- Ready --> Closed: close()
- Closed --> Ready: init()