Search code examples
pythonoracle-databasecx-oracle

Using multiple hosts on cx_oracle


I'm using the cx_oracle pip package to connect to a oracle DB at work.

dsn_tns = cx_Oracle.makedsn('1.2.3.4', '1521', service_name='TRADES')
conn = cx_Oracle.connect(user='analytics_user', password='welcome', dsn=dsn_tns)
c = conn.cursor()
c.execute('SELECT * FROM all_tables')

However the oracle db I connect to has 2 host IP addresses.

The original connection string I was given was like this;

SERVICE  =
   (DESCRIPTION =
     (ADDRESS = (PROTOCOL = TCP)(HOST = 1.2.3.4)(Port=1521))
     (ADDRESS = (PROTOCOL = TCP)(HOST = 1.222.33.44)(Port=1521))
     (CONNECT_DATA =
       (SERVICE_NAME = TRADES)
     )
   )

Is there anyway to use both on cx_oracle? or is there any other package I can use that will allow that? SQL_Alchemy?


Solution

  • you can use the DSN string as is, without the SERVICE =:

    DSN = """
       (DESCRIPTION =
         (ADDRESS = (PROTOCOL = TCP)(HOST = 1.2.3.4)(Port=1521))
         (ADDRESS = (PROTOCOL = TCP)(HOST = 1.222.33.44)(Port=1521))
         (CONNECT_DATA =
           (SERVICE_NAME = TRADES)
         )
       )
    """
    
    with cx_Oracle.connect(user='user', password='pass', dsn=dsn_tns) as conn:
        c = conn.cursor()
        c.execute('SELECT * FROM all_tables')
    

    thanks @Christopher-Jones here the details oracle-net-connect-descriptor-strings