Search code examples
python-3.xdatabaseamazon-web-servicesoracle

Update Onprem oracle table from aws glue python shell using oracledb


I have a usecase that needs to update an onprem table using the aws glue python shell. Please note i want to use python-oracledb in thin mode

  1. I have installed the oracledb
  2. I dba has set up a jdbc url to my onprem table accessible from aws cloud
  3. when i try to run the python shell job , it is throwing databse error DPY-4027: no configuration directory to search for tnsnames.ora

To my understanding it shouldnt look for the "tnsnames.ora" as i am using jdbc url . what is the way to look for the code to utilize the thin url below is the code i had written. Any suggestions or errors please let me know

import sys
import boto3
import oracledb
import ast
    # Oracle connection details using Easy Connect string

session = boto3.session.Session()
client = session.client(service_name='secretsmanager')   

    
def get_secret(secret_name):
    # Create a Secrets Manager client
    try:
        get_secret_value_response = client.get_secret_value(SecretId=secret_name)
        return get_secret_value_response
    except Exception as e:
        raise 


def update_table(table_name):
    try :
        print("im here update table")
        cred=get_secret("my_secret")
        cred=ast.literal_eval(cred["SecretString"])
        print(f"credentials {cred}")
        user =cred["username"]
        password =cred["password"]
        conn_str = 'jdbc:oracle:thin://@my_host:my_port/my_service'
        conn = oracledb.connect(user=user, password=password,dsn=conn_str)
        cursor = conn.cursor()
        cursor.execute("UPDATE my_table SET flag ='N' WHERE column ='xyz'")
        conn.commit()
        cursor.close()
        conn.close()
        return {
            'statusCode': 200,
            'body': json.dumps('Update successful')
        }
    except Exception as e:
        raise 
    
    
if __name__ == '__main__':
    
    return_code = update_table("my_table")
  1. I have installed the oracledb
  2. I dba has set up a jdbc url to my onprem table accessible from aws cloud
  3. when i try to run the python shell job , it is throwing databse error DPY-4027: no configuration directory to search for tnsnames.ora
  4. I checked the url connectivity , they all looked good as the same url being used in other jobs

Solution

  • Python is not Java and the connection strings differ. See the python-oracledb doc JDBC and Oracle SQL Developer Connection Strings for how to map JDBC connection strings into something usable by python-oracledb.

    You almost certainly want to use:

     conn_str = 'my_host:my_port/my_service'
    

    Also see With python-oracledb what does 'DPY-4027: no configuration directory to search for tnsnames.ora' mean