Alice Chiu
Alice Chiu

Reputation: 123

cx_Oracle._Error in Python script

I have attempted to search for a resolution or at least a few troubleshooting steps to understand why I see these errors when I execute a Python script. No luck so far. I SSH to Rackspace to do this.

The errors reference the Oracle db. To be more specific, it is cx_Oracle._Error and cx_Oracle.DatabaseError on separate occasions. Below is an example of a few lines from the server logs. It does not give any more information than that.

Running against Oracle server: prod.somecompanyname + prod123
Error connecting to databases: (<cx_Oracle._Error object at0x7ffff7fe9af8>,) 
Error verifying existing Oracle records

My colleague is able to execute the script successfully and does not encounter the error. I compared our .bash_profile and .bashrc and nothing different stands out. The Oracle server credentials are correct in the script as well as the Oracle environment path. This may be isolated to something on my end however I cannot figure out where.

Any suggestions on where to look to fix this is appreciated.

def oraclerecords(logger, env, db1Pass, db2Pass, db3Pass, verifyRecordset):
  import cx_Oracle
  retval = None

  try:
    db1UID='somedb1name'
    db2UID='somedb2name'
    if env == 'p':
      dbServer='prod.somecompanyname.com'
      dbSID='SIDPR'
    elif env == 's':
      dbServer='stage.somecompanyname.com'
      dbSID='SIDSTG'
    elif env == 'r':
      dbServer='stage.somecompanyname.com'
      dbSID='SIDDEV'

    db3UID = 'somedb3name'
    db3SID = 'db3HUB'

    logger.info('Running against Oracle server:' + dbServer + ' SID:' + dbSID)
    connString = (db1UID + '/' + db1Pass + '@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS='
      '(PROTOCOL=TCP)(HOST=' + dbServer + ')(PORT=1234)))(CONNECT_DATA=(SID=' + dbSID + ')))')
    conndb1 = cx_Oracle.connect(connString)
    curdb1 = conndb1.cursor()
    curdb0 = conndb1.cursor()

    connString = (db2UID + '/' + db2Pass + '@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS='
      '(PROTOCOL=TCP)(HOST=' + dbServer + ')(PORT=1234)))(CONNECT_DATA=(SID=' + dbSID + ')))')
    conndb2 = cx_Oracle.connect(connString)
    curdb2 = conndb2.cursor()

    connString = (db3UID + '/' + db3Pass + '@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS='
      '(PROTOCOL=TCP)(HOST=prod.atsomecompany.com)(PORT=1234)))(CONNECT_DATA=(SID=' + db3SID + ')))')
    conndb3 = cx_Oracle.connect(connString)
    curdb3 = conndb3.cursor()
  except Exception as e:
    logger.error('Error connecting to databases: ' + str(e.args))
    return verifyRecordset, 2

Upvotes: 0

Views: 5809

Answers (1)

Luke Woodward
Luke Woodward

Reputation: 65054

The issue with your Python script is on this line:

  logger.error('Error connecting to databases: ' + str(e.args))

Perhaps the simplest way to improve it is to replace it with the following:

  logger.error('Error connecting to databases: ' + str(e))

I wrote the following short Python script that attempts to connect to an Oracle XE database:

import cx_Oracle

connect_string = "..."
try:
    conn = cx_Oracle.connect(connect_string)
    print "Got connection"
except Exception as e:
    print str(e.args)
    print str(e)

I knew this script would raise an exception because the database and the listener it was attempting to connect to were both down. When I ran, it I got the following output:

(<cx_Oracle._Error object at 0x02354308>,)
ORA-12541: TNS:no listener

The first line here doesn't tell me anything helpful, but the second line contains a more useful message.

Hopefully after making this change to your script you will see a more useful error message, which should help you track down what the real problem is here.

Upvotes: 2

Related Questions