{"slug": "connect-a-fastapi-app-to-oracle-ai-database-thin-mode-wallet-a-real-pool-step-by", "title": "Connect a FastAPI App to Oracle AI Database — Thin Mode + Wallet, a Real Pool, Step by Step", "summary": "A developer published a step-by-step guide showing how to connect a FastAPI application to Oracle AI Database using python-oracledb in Thin mode, a downloaded wallet, and a connection pool created with create_pool_async inside FastAPI's lifespan. The walkthrough covers provisioning an Always Free Autonomous Database (26ai), loading credentials from environment variables, and inserting and reading back rows in a sample DevShelf table. The author notes that Thin mode requires no Oracle Client libraries, replacing the older cx_Oracle client install.", "body_md": "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.\n\n**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.\n\nOracle 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).\n\nIn 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.\n\nBecause 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.\n\nInside, Thin mode reads just two files:\n\n`tnsnames.ora` — the address book`ewallet.pem` — the key\n⚠️ **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.\n\nA **driver** is the translator between Python and the database. Ours is **python-oracledb**:\n\n```\npip install oracledb fastapi uvicorn\n```\n\nThat 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.\n\nHand 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:\n\n``` python\nimport os\n\nimport oracledb\n\nWALLET_DIR = os.environ[\"ORACLE_WALLET_DIR\"]\n\nconnection = oracledb.connect(\n    user=os.environ[\"ORACLE_USER\"],\n    password=os.environ[\"ORACLE_PASSWORD\"],\n    dsn=os.environ.get(\"ORACLE_DSN\", \"vsldb_tp\"),\n    config_dir=WALLET_DIR,\n    wallet_location=WALLET_DIR,\n    wallet_password=os.environ[\"ORACLE_WALLET_PASSWORD\"],\n)\nprint(\"Connected — server version:\", connection.version)\n```\n\nRun it:\n\n```\nConnected — server version: 23.26.4.1.0\n```\n\nThat version is read back **from the server itself** — 23.26 is the internal number of the 26ai release.\n\nA 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:\n\n```\nInserted 3 rows. Reading them back:\n  (1, 'python-oracledb documentation', 'https://python-oracledb.readthedocs.io', 'driver')\n  (2, 'FastAPI documentation', 'https://fastapi.tiangolo.com', 'framework')\n  (3, 'Autonomous Database documentation', 'https://docs.oracle.com/en/cloud/paas/autonomous-database/', 'database')\n```\n\nOne 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.\n\nWhy 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.\n\nThree 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:\n\n``` bash\n$ curl -X POST http://127.0.0.1:8011/items -d @item4.json\n{\"id\":4,\"title\":\"Oracle AI Database@Azure overview\",\"url\":\"https://learn.microsoft.com/en-us/azure/oracle/oracle-db/\",\"tag\":\"azure\"}\n```\n\nDo 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.\n\nWe 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.**\n\nThe 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.**\n\n*The Network Anchors pane in our free account — empty for now: no virtual networks yet.*\n\nOne pip install · a wallet that works like a key · a five-line connection · a pool born once at startup · proof inside the database itself.\n\n**The interview version:** start Thin, pool at startup, guard the wallet like a password, place the app beside the database.\n\nThe 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).\n\n**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.\n\n*Originally published on [Visual Studio Learn](https://www.visualstudiolearn.com/2026/11/connect-fastapi-app-to-oracle-ai.html).*", "url": "https://wpnews.pro/news/connect-a-fastapi-app-to-oracle-ai-database-thin-mode-wallet-a-real-pool-step-by", "canonical_source": "https://dev.to/visualstudiolearn/connect-a-fastapi-app-to-oracle-ai-database-thin-mode-wallet-a-real-pool-step-by-step-2h7f", "published_at": "2026-11-25 17:39:00+00:00", "updated_at": "2026-11-25 17:44:33.294786+00:00", "lang": "en", "topics": ["developer-tools", "ai-tools", "ai-infrastructure"], "entities": ["Oracle", "FastAPI", "python-oracledb", "Oracle AI Database", "Oracle Cloud", "Microsoft Azure", "cx_Oracle"], "also_reported_by": [], "takeaway": "python-oracledb's default Thin mode connects to Oracle AI Database with only a pip install, a wallet folder (not the zip) and its separate wallet password, and a pool built once via create_pool_async in FastAPI's lifespan (min 2, max 5) avoids per-request handshakes.", "recommendation": {"advice": "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.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/connect-a-fastapi-app-to-oracle-ai-database-thin-mode-wallet-a-real-pool-step-by", "markdown": "https://wpnews.pro/news/connect-a-fastapi-app-to-oracle-ai-database-thin-mode-wallet-a-real-pool-step-by.md", "text": "https://wpnews.pro/news/connect-a-fastapi-app-to-oracle-ai-database-thin-mode-wallet-a-real-pool-step-by.txt", "jsonld": "https://wpnews.pro/news/connect-a-fastapi-app-to-oracle-ai-database-thin-mode-wallet-a-real-pool-step-by.jsonld"}}