Jeremy
Jeremy

Reputation: 11

Python App connection to Oracle Autonomous Database

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.

Upvotes: 0

Views: 146

Answers (0)

Related Questions