Introduction :
The error “DPY-4011: The database or network closed the connection” can occur when attempting to connect to an Oracle Database using the python-oracledb Thin mode. This issue often arises when Native Network Encryption (NNE) is enabled, as NNE requires the Thick mode to function properly. In this blog, we’ll explore the root cause of this error and provide a step-by-step guide to resolve it, ensuring a stable and secure database connection.
Error Message: “DPY-4011: the database or network closed the connection”
Understanding DPY-4011 :
The DPY-4011 error indicates that the connection failed due to incompatibilities between the Thin mode of python-oracledb and the database’s NNE settings. Thin mode, while lightweight and easy to use, does not support NNE. Switching to Thick mode resolves the issue by enabling NNE support.
Prerequisites for Troubleshooting:
Verify if NNE is Enabled Run the following query to check if NNE is enabled in your database with the following querry.
SQL> SELECT network_service_banner FROM v$session_connect_info;
If NNE is enabled, the output will display encryption and crypto-checksumming services. If not, only basic services will appear.
Steps to Resolve DPY-4011:
Step 1: Install Oracle Client Libraries Download and install the Oracle Instant Client on your system. For example:
Version: 19.19.0.0.0 Download Link: https://download.oracle.com/otn_software/linux/instantclient/1919000/instantclient-basic-linux.x64-19.19.0.0.0dbru.el9.zip
Extract the contents to a directory, e.g., /opt/oracle/instantclient
Step 2:
Configure Environment Variables Update your shell profile (e.g., ~/.bashrc) to include the Oracle Instant Client library paths:
export LD_LIBRARY_PATH=/opt/oracle/instantclient:$LD_LIBRARY_PATH export PATH=/opt/oracle/instantclient:$PATH
Reload the profile:
source ~/.bashrc
Step 3:
Enable Thick Mode in Your Python Script Modify your Python script to initialize the Oracle Client in Thick mode. Example:
import oracledb oracledb.init_oracle_client(lib_dir=”/opt/oracle/instantclient”)
Run your script in a new session to apply the changes:
nohup python your_script.py &
Conclusion:
The DPY-4011 error, caused by Native Network Encryption requirements, is resolved by enabling Thick mode in python-oracledb and configuring the Oracle Instant Client. By following the steps outlined in this guide, you can ensure reliable and secure connections to your Oracle Database. Proactive monitoring and best practices will further enhance the stability of your database applications.