Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

python-oracledb loses its Oracle connection with DPY-4011

Your error

oracledb.exceptions.DatabaseError: DPY-4011: the database or network closed the connection1 more line it can show

Fix it fast

Most common cause, 1 of 3

The database enforces Native Network Encryption, and Thin mode cannot use it

Turn on Thick mode, which needs the Oracle Client libraries (for example Instant Client) and one call before the first connection.

Fix
import oracledb

# lib_dir is for Windows and macOS; on Linux, put Instant Client on the
# library search path (ldconfig) and call it with no arguments
oracledb.init_oracle_client(lib_dir=r"C:\oracle\instantclient_23_5")
conn = oracledb.connect(user="hr", password=pw, dsn="dbhost.example.com/orclpdb")
print(conn.version)
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

DPY-4011: the database or network closed the connection means Oracle dropped the link. At connect time, python-oracledb 2.x and older in Thin mode showed it when the database enforces Native Network Encryption; newer versions say DPY-3001, and Thick mode or TLS fixes it. Otherwise use a small fixed-size pool and keep-alives. Applies to python-oracledb 26.1.

  • Applies to python-oracledb 26.1.0 (Python 3.10+), Thin and Thick modes, Oracle Database 12.1 and later
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the driver version, then note when the error appears. On every first connect with version 2.x or older, check cause 1; only under load or while a pool grows, cause 2; only after the app sat idle, cause 3.

Run this first
python -c "import oracledb; print(oracledb.__version__)"

Then find the line you see in this list and open its fix:

  1. 1The database enforces Native Network Encryption, and Thin mode cannot use itEvery connect attempt fails at once on python-oracledb 2.x or older, and the banner query shows an encryption or crypto-checksumming adapter in use
  2. 2The pool grows faster than the database can accept connectionsMost requests work, and DPY-4011 shows up in bursts at start-up or traffic peaks when a dynamic pool adds connections
  3. 3A connection that sat idle was closed underneath the appThe first query after a quiet period fails with DPY-4011, and a retry works

What does the error look like?

  • oracledb.exceptions.DatabaseError: DPY-4011: the database or network closed the connection
  • DPY-3001: Native Network Encryption and Data Integrity is only supported in python-oracledb thick mode

Cause 1 of 3

The database enforces Native Network Encryption, and Thin mode cannot use it

You seeEvery connect attempt fails at once on python-oracledb 2.x or older, and the banner query shows an encryption or crypto-checksumming adapter in use

Check

On python-oracledb 2 or earlier in Thin mode, DPY-4011 at connect time is most often Native Network Encryption (NNE) or checksumming switched on in the database. Thin mode supports neither. Later releases name the problem as DPY-3001, so an upgrade clarifies the message but does not make Thin mode connect.

Check
-- run in SQL*Plus or SQL Developer, which can connect
SELECT network_service_banner FROM v$session_connect_info;

Fix

Turn on Thick mode, which needs the Oracle Client libraries (for example Instant Client) and one call before the first connection. The other route is to have the DBA replace NNE with TLS, which both modes support.

Fix
import oracledb

# lib_dir is for Windows and macOS; on Linux, put Instant Client on the
# library search path (ldconfig) and call it with no arguments
oracledb.init_oracle_client(lib_dir=r"C:\oracle\instantclient_23_5")
conn = oracledb.connect(user="hr", password=pw, dsn="dbhost.example.com/orclpdb")
print(conn.version)

Verify

The connect call returns and prints the database version instead of raising DPY-4011 or DPY-3001.

Verify
python app.py

Confirmed by python-oracledb.readthedocs.io

Did this fix it?

Cause 2 of 3

The pool grows faster than the database can accept connections

You seeMost requests work, and DPY-4011 shows up in bursts at start-up or traffic peaks when a dynamic pool adds connections

Check

python-oracledb's pool guide warns that when a dynamically sized pool needs to grow and database resources are limited, the database may simply drop new connection attempts, which surfaces as DPY-4011. Several processes that each open a large pool make the same storm.

Check
grep -rn "create_pool(" --include=*.py .

Fix

Use a small pool with min equal to max, as the Oracle Real-World Performance Group recommends, so any shortage shows up when the pool is created rather than mid-traffic. When many Python processes each need a pool, Database Resident Connection Pooling (DRCP) is the documented option.

Fix
pool = oracledb.create_pool(
    user="hr", password=pw, dsn="dbhost.example.com/orclpdb",
    min=4, max=4, increment=1,
)
conn = pool.acquire()

Verify

Load the app the way that failed before; acquire() keeps returning connections and the bursts of DPY-4011 stop.

Verify
python -c "import app; print(app.pool.opened, app.pool.busy)"

Confirmed by python-oracledb.readthedocs.io

Did this fix it?

Cause 3 of 3

A connection that sat idle was closed underneath the app

You seeThe first query after a quiet period fails with DPY-4011, and a retry works

Check

When DPY-4011 is raised on a connection that was already open, the docs say the extra lines that follow it may name the reason. A pool's acquire() checks that the network transport is still open and pings connections that sat idle for ping_interval seconds, while a single long-lived connect() call gets neither check.

Check
grep -rnE "oracledb.connect\(|ping_interval|expire_time" --include=*.py .

Fix

Get connections from a pool instead of holding one open for hours, and add expire_time to the Easy Connect string to send keep-alive probes. Also catch errors after each execute(), because a session can still end between acquire() and the query.

Fix
pool = oracledb.create_pool(
    user="hr", password=pw,
    dsn="dbhost.example.com/orclpdb?expire_time=2",
    min=2, max=2, increment=1, ping_interval=60,
)

Verify

Leave the app idle longer than the gap that used to break it, then run a query; it succeeds without DPY-4011.

Verify
python -c "import app; print(app.pool.acquire().cursor().execute('select 1 from dual').fetchone())"

Confirmed by python-oracledb.readthedocs.io

Did this fix it?

How do I stop it coming back?

  • Ask the DBA whether NNE or checksumming is on before choosing Thin mode for a new service.
  • Create pools once at start-up with min equal to max, and size them against the database's session limit.
  • Put expire_time in the connect string of every long-running service, so quiet connections still send keep-alive probes.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.