Lifecycle and Shutdown

Last updated


This page covers the full pool lifecycle, from creation in init() to shutdown through close().

init()


  1. Resolves the read and write configs by priority (see Configuration)
  2. Creates a pool with createPool() for each side whose database is non-empty
  3. Checks out one connection from each pool and releases it to verify credentials and network
  4. 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.
  1. import MySQLPool from "@pardnchiu/mysql-pool";
  2. await MySQLPool.init();
  3. try {
  4. await MySQLPool.table("jobs", "write").where("status", "pending").get();
  5. } finally {
  6. await MySQLPool.close();
  7. }

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.
ItemBehavior
RegisteredOnce, when the module is first loaded
Actionawait MySQLPool.close() → process.exit(0)
Exit codeAlways 0
Your own listenersRun 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()
  1. stateDiagram-v2
  2. [*] --> Uninitialized
  3. Uninitialized --> Ready: init() succeeds
  4. Uninitialized --> Uninitialized: init() fails and throws
  5. Ready --> Closed: close()
  6. Closed --> Ready: init()


中文