0

I have an Python application that connects to an Oracle Autonomous database. This application was working correctly until I had to migrate my Oracle Autonomous database from 23c to 23ai. The app continues to works correctly on my Mac, but it will not work on my PC. On my Oracle Autonomous Database I have "Mutual TLS (mTLS) authentication: Not required" which is the correct setting to be able to connect to the database without a wallet. I also have the Access control list set to my IP address by using the "Add my IP Address" feature from Oracle Cloud. I am able to connect to the database using SQL Developer on my PC.

I tried running the following code. I would expect that console would print "hello world" but instead I got an error "An error occurred while connecting to Oracle: DPY-4011: the database or network closed the connection"

import os
import oracledb

def check_oracle_connection():
    try:
        # Load the environment variables
        username = os.getenv('ORACLE_AUTO_USERNAME')
        password = os.getenv('ORACLE_AUTO_PASSWORD')
        dsn = os.getenv('ORACLE_AUTO_DSN')

        # Check if environment variables are not set
        if not username or not password or not dsn:
            raise EnvironmentError("One or more environment variables for Oracle credentials are not set")

        # Construct the connection string
        connection = oracledb.connect(user=username, password=password, dsn=dsn)

        # Create a cursor object
        cursor = connection.cursor()

        # Execute a simple query to check the connection
        cursor.execute("SELECT 'hello world' FROM dual")
        result = cursor.fetchone()

        # Print the result
        print(result[0])
    
    except oracledb.DatabaseError as e:
        # Print out the exception message
        print("An error occurred while connecting to Oracle:", e)

    except EnvironmentError as e:
        # Print out the exception message for environment variable errors
        print(e)

    finally:
        try:
            # Close the cursor and connection
            cursor.close()
            connection.close()
            print("Oracle connection is closed")
        except:
            pass

check_oracle_connection()

I also tried this and got the following output ping adb.us-phoenix-1.oraclecloud.com

Pinging adb.us-phoenix-1.oci.oraclecloud.com [192.29.105.116] with 32 bytes of data:

Request timed out.

3
  • If you can connect from SQLDeveloper means that there is a parameter difference between the 2, the first suspect would be ORACLE_AUTO_DSN, since it's a shell environment variable either you forgot to modify it, either you modify it in the source file but forget to reload it in your shell session... so dump it from your Python script and check it again. Commented Jun 10, 2024 at 7:11
  • The parameters are the same as what I am using with SQL Developer. I have hard coded the username, password and dsn in the Python script (copied directly for Oracle Cloud). To test if there is potentially an issue with the 23ai database, I provisioned a 19c database and tested the Python script and was able to connect to the 19c database without issues. There seems to be an issue with the 23ai database. Commented Jun 11, 2024 at 22:30
  • The ACL settings of the 23 DB ? Commented Jun 12, 2024 at 5:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.