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 showFix it fast
Most common cause, 1 of 3The 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.
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)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.
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.
python -c "import oracledb; print(oracledb.__version__)"Then find the line you see in this list and open its fix:
- 1The database enforces Native Network Encryption, and Thin mode cannot use it
Every 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 - 2The pool grows faster than the database can accept connections
Most requests work, and DPY-4011 shows up in bursts at start-up or traffic peaks when a dynamic pool adds connections - 3A connection that sat idle was closed underneath the app
The 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 connectionDPY-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.
-- 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.
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.
python app.pyConfirmed by python-oracledb.readthedocs.io
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.
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.
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.
python -c "import app; print(app.pool.opened, app.pool.busy)"Confirmed by python-oracledb.readthedocs.io
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.
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.
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.
python -c "import app; print(app.pool.acquire().cursor().execute('select 1 from dual').fetchone())"Confirmed by python-oracledb.readthedocs.io
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.