Level 6 / Project 01 - SQL Connection Simulator¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
- connection config and retry patterns
Why this project exists¶
This project gives you level-appropriate practice in a realistic operations context. Goal: run the baseline, alter behavior, break one assumption, recover safely, and explain the fix.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
cd <repo-root>/projects/level-6/01-sql-connection-simulator
python project.py --input data/sample_input.txt --output data/output_summary.json
pytest -q
Expected terminal output¶
{
"rows_inserted": 7,
"rows": [ ... ],
"health": {"status": "healthy", "sqlite_version": "..."},
"pool_stats": {"created": 1, "reused": 1, ...}
}
Expected artifacts¶
data/output_summary.json— full run results with pool stats- Passing tests (
pytest -q→ 8+ passed) - Updated
notes.md
Alter it (required)¶
- Add a
--timeoutCLI flag that setsConnectionConfig.timeout(observe what happens with very small values like 0.001). - Track peak concurrent connections (the highest number of connections checked out simultaneously) and include it in
pool.stats(). - Add a
ConnectionPool.shrink()method that closes idle connections down to a target count. - Re-run script and tests after each change.
Break it (required)¶
- Set
pool_size=0and observe what happens when connections cannot be pooled. - Pass an invalid database path (e.g.
/nonexistent/dir/db.sqlite) and observe the retry/failure behaviour. - Close a connection manually, then return it to the pool — what happens on the next acquire?
Fix it (required)¶
- Add a guard in
release()that pings the connection before returning it to the pool (discard broken ones). - Validate that
pool_size >= 1inConnectionConfig.__post_init__. - Add tests for each broken case above.
Explain it (teach-back)¶
- Why do context managers (
withblocks) matter for database connections? - What is the performance difference between creating a new connection per query vs. pooling?
- Why does the retry use exponential backoff instead of a fixed delay?
- In a production web server, what problems arise if the pool is too small? Too large?
Mastery check¶
You can move on when you can: - run baseline without docs, - explain one core function line-by-line, - break and recover in one session, - keep tests passing after your change.
Related Concepts¶
- Collections Explained
- Decorators Explained
- Errors and Debugging
- Files and Paths
- Quiz: Collections Explained
| ← Prev | Home | Next → |
|---|---|---|