# Lifecycle and Shutdown

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](/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.

```typescript
import MySQLPool from "@pardnchiu/mysql-pool";

await MySQLPool.init();
try {
  await MySQLPool.table("jobs", "write").where("status", "pending").get();
} finally {
  await MySQLPool.close();
}
```

## States

```mermaid
stateDiagram-v2
    [*] --> Uninitialized
    Uninitialized --> Ready: init() succeeds
    Uninitialized --> Uninitialized: init() fails and throws
    Ready --> Closed: close()
    Closed --> Ready: init()
```

## Related Pages

- [Read-Write Pools](/read-write-pools)
- [Configuration](/configuration)
