Advanced
Checkpoint 3 — pooled multi-worker loader
You should now be able to manage transactions explicitly, pool connections, and run OCILIB from multiple threads safely.
The exercise — parallel bulk insert
Extend your employee-directory CLI into a parallel importer:
- Create
OCI_ENV_THREADEDenvironment and aOCI_POOL_CONNECTIONpool (min=2, max=4, incr=1) withOCI_PoolSetStatementCacheSize(pool, 20). - Read a CSV file (
empno,ename,job,sal,deptno— invent ~200 rows) on the main thread into an in-memory list. - Spawn 4 worker threads. Each worker:
- checks out its own connection from the pool,
- prepares
insert into employees … values(:n,:e,:j,:s,:d)once, - loops over its share of rows —Executeeach,OCI_ConnectionCommitevery 25 rows, - releases the connection and reports rows done + elapsed. - Main thread joins all workers, prints
OCI_PoolGetBusyCount/GetOpenedCountstats, verifiesselect count(*)grew by 200.
Acceptance checks
- Rows are all there once —
select empno, count(*) … having count(*)>1returns nothing. - With a deliberate duplicate
empnoin the CSV, exactly one worker's commit fails and its rollback is clean — other workers finish fine. busynever exceedsopenedormax.
Stretch goals
- Swap
OCI_POOL_CONNECTION→OCI_POOL_SESSION, tag sessions, confirm reuse. - Wrap each worker's 25-row block in an
OCI_Transactionobject instead of connection-level commit. - Have a 5th thread call
OCI_ConnectionBreakon a long-running query and log ORA-01013.
What it exercises
| Skill | Page |
|---|---|
| Transactions & boundaries | 410/415 |
| Pool create/checkout/monitor | 420/425 |
| Threaded env + sharing rules | 430 |
| Prepared execute loops | 260 |
| Error handling per thread | 080 |
Part 5 done. Next: Part 2 — the Oracle data-type toolbox, starting with strings and charsets.