# Connect a FastAPI App to Oracle AI Database — Thin Mode + Wallet, a Real Pool, Step by Step

> Source: <https://dev.to/visualstudiolearn/connect-a-fastapi-app-to-oracle-ai-database-thin-mode-wallet-a-real-pool-step-by-step-2h7f>
> Published: 2026-11-25 17:39:00+00:00

**Recommendation:** Build the Oracle connection pool once at startup with create_pool_async inside FastAPI's lifespan and borrow connections per request, keeping wallet and DB credentials in environment variables.
More in the playbook "Running AI in production": <https://wpnews.pro/playbooks/production-ops>

Your FastAPI app is useless without a database — and connecting Python to Oracle used to mean installing a heavy client and hunting for config files. Not anymore. One pip install, a small key file called a wallet, and a real connection pool. By the end of this post your app will answer live requests from a real Oracle database — and you will have proof inside the database itself.

**The map** — everything follows it: a user sends a request → it lands on our FastAPI app → the app borrows a connection from a **pool** (connections kept open and ready, like taxis at a rank) → the pool talks to **Oracle AI Database**, which stores our rows and answers questions in **SQL**. Every step below lights one leg of this map.

Oracle AI Database is Oracle's database service in the cloud: you install nothing; Oracle runs it, you just connect. The same product also lives inside Microsoft Azure, called **Oracle AI Database@Azure** — we walk into that room in Step 11. For our real run we use the free edition: an **Always Free Autonomous Database** (autonomous = it patches and backs itself up).

In the Oracle Cloud console, create an Autonomous Database: Transaction Processing workload, **Always Free**, version **26ai**. Ours sits in the Montreal region. When the console shows **State: Available**, you are ready.

Because this database lives on the internet, it must be sure you are really you. From the console's **DB Connection** panel, download the **wallet** — a small zip of key files. **Unzip it: the driver reads the folder, not the zip.** Keep it private and never share it — the wallet is a key to your database, exactly like a password.

Inside, Thin mode reads just two files:

`tnsnames.ora` — the address book`ewallet.pem` — the key
⚠️ **The one warning that saves everyone an hour:** the wallet has *its own password*. That is **not** your database password. Two different passwords, two different jobs.

A **driver** is the translator between Python and the database. Ours is **python-oracledb**:

```
pip install oracledb fastapi uvicorn
```

That is the entire client install. The driver runs in **Thin mode** by default: pure Python, no Oracle Client libraries. The old driver, cx_Oracle, needed a full client install — that world is gone.

Hand the driver four things — the user name, the database password, the address from the wallet's address book, and the wallet folder with its password. Credentials come from **environment variables**, never typed into code:

``` python
import os

import oracledb

WALLET_DIR = os.environ["ORACLE_WALLET_DIR"]

connection = oracledb.connect(
    user=os.environ["ORACLE_USER"],
    password=os.environ["ORACLE_PASSWORD"],
    dsn=os.environ.get("ORACLE_DSN", "vsldb_tp"),
    config_dir=WALLET_DIR,
    wallet_location=WALLET_DIR,
    wallet_password=os.environ["ORACLE_WALLET_PASSWORD"],
)
print("Connected — server version:", connection.version)
```

Run it:

```
Connected — server version: 23.26.4.1.0
```

That version is read back **from the server itself** — 23.26 is the internal number of the 26ai release.

A connection that stores nothing is a phone line to an empty room. Our app is **DevShelf** — it saves developer reference links. Its table is `shelf_items` (an id, a title, a link, a tag). We insert three real rows — the documentation pages for this very tutorial — and a SELECT reads them straight back:

```
Inserted 3 rows. Reading them back:
  (1, 'python-oracledb documentation', 'https://python-oracledb.readthedocs.io', 'driver')
  (2, 'FastAPI documentation', 'https://fastapi.tiangolo.com', 'framework')
  (3, 'Autonomous Database documentation', 'https://docs.oracle.com/en/cloud/paas/autonomous-database/', 'database')
```

One connection is fine for a script; it is wrong for a web app. Opening a connection is slow — a handshake, a login, every single time. So the app builds a **pool once, when it starts**, with `create_pool_async` (min 2, max 5), inside FastAPI's **lifespan** — the code that runs once at startup. When the app shuts down, the same lifespan code **closes the pool**, so no borrowed connection is left hanging.

Why so small? Our free database allows **30 sessions at once** — the honest ceiling — and two to five is plenty here. Every request borrows a connection through a **dependency** (FastAPI's way of handing each request what it needs), uses it, and gives it back.

Three endpoints: `/health` (asks the database a one-line question), `POST /items`, `GET /items`. Start the server and ask it for the items — the same three rows, served over the web, out of the database. Then post a fourth through the API:

``` bash
$ curl -X POST http://127.0.0.1:8011/items -d @item4.json
{"id":4,"title":"Oracle AI Database@Azure overview","url":"https://learn.microsoft.com/en-us/azure/oracle/oracle-db/","tag":"azure"}
```

Do not take the API's word for it. In **SQL Developer Web** — the database's own screen — run the same SELECT: all four rows are there, including the one that arrived through the web API a moment ago. The write really landed.

We timed our own run — from our machine to the free database in Montreal — and one round trip (a one-line query over a pooled connection, median of 15) took **about 100 milliseconds**. That number measures *our distance*, nothing more — your number will differ. The rule it teaches is the real lesson: **keep the app next to the database. Distance is the one cost no code can optimise away.**

The identical database product can live inside your own Azure network, as **Oracle AI Database@Azure**. Your **virtual network** (your private address space in Azure) gives the database a **delegated subnet** — a reserved street where only Oracle doors may open — so your app reaches it on a private address, with no trip across the public internet. One honest note: creating the database there is **paid**, so we browse only. What matters: **your code from today does not change. Only the address in the wallet changes.**

*The Network Anchors pane in our free account — empty for now: no virtual networks yet.*

One pip install · a wallet that works like a key · a five-line connection · a pool born once at startup · proof inside the database itself.

**The interview version:** start Thin, pool at startup, guard the wallet like a password, place the app beside the database.

The complete DevShelf code — connection test, table setup, the pooled FastAPI app, and the timing script — lives in the companion repo folder [topic-11-oracle-fastapi](https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-11-oracle-fastapi).

**My question for you:** your app and your database — are they sitting next to each other, or is every request taking a road trip? Tell me in the comments where your database lives.

*Originally published on [Visual Studio Learn](https://www.visualstudiolearn.com/2026/11/connect-fastapi-app-to-oracle-ai.html).*
