1

I am trying to create a Docker container that connects to a VPN and then checks that it can connect to some databases. Currently I'm using the following code:

DB_URI_PREFIX = {
    'mssql': 'mssql+pymssql://', 'oracle': 'oracle+cx_oracle://', 'postgres': 'postgres://',
}


def connect_to_db(db_type: str, host: str, username: str, password: str, database: str):
    db_uri = '{}{}:{}@{}/{}'.format(DB_URI_PREFIX[db_type], username, password, host, database)
    engine = create_engine(db_uri)
    connection = engine.connect()
    print('Successfully connected to {}/{}'.format(host, database))
    connection.close()

db_uri in this case ends up being mssql+pymssql://user:pass@host:port_num/database_name for an SQL Server database and oracle+cx_oracle://user:pass@host:port_num/database_name for an Oracle database.

The above works fine for the SQL server and Postgres databases but for the Oracle one I get this error:

 Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 2158, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 345, in unique_connection
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 782, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 532, in checkout
    rec = pool._do_get()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 1186, in _do_get
    self._dec_overflow()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/compat.py", line 187, in reraise
    raise value
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 1183, in _do_get
    return self._create_connection()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 350, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 477, in __init__
    self.__connect(first_connect_check=True)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 667, in __connect
    connection = pool._invoke_creator(self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/strategies.py", line 105, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/default.py", line 410, in connect
    return self.dbapi.connect(*cargs, **cparams)
cx_Oracle.DatabaseError: ORA-12545: Connect failed because target host or object does not exist

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "connect_to_db.py", line 37, in <module>
    connect_to_db(args.db_type, args.host, args.username, args.password, args.database)
  File "connect_to_db.py", line 16, in connect_to_db
    connection = engine.connect()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 2102, in connect
    return self._connection_cls(self, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 90, in __init__
    if connection is not None else engine.raw_connection()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 2188, in raw_connection
    self.pool.unique_connection, _connection)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 2162, in _wrap_pool_connect
    e, dialect, self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 1476, in _handle_dbapi_exception_noconnection
    exc_info
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/compat.py", line 186, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/base.py", line 2158, in _wrap_pool_connect
    return fn()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 345, in unique_connection
    return _ConnectionFairy._checkout(self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 782, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 532, in checkout
    rec = pool._do_get()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 1186, in _do_get
    self._dec_overflow()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/util/compat.py", line 187, in reraise
    raise value
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 1183, in _do_get
    return self._create_connection()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 350, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 477, in __init__
    self.__connect(first_connect_check=True)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/pool.py", line 667, in __connect
    connection = pool._invoke_creator(self)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/strategies.py", line 105, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/default.py", line 410, in connect
    return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.DatabaseError: (cx_Oracle.DatabaseError) ORA-12545: Connect failed because target host or object does not exist (Background on this error at: http://sqlalche.me/e/4xp6)

Initially I thought this meant that the host could not be found but it responds when pinging it. Also, bizarrely, if I connect using cx_Oracle directly with an 'Oracle style' connection string and give that to SQLAlchemy, I can connect:

def get_oracle_connection(conn_str: str):
    return cx_Oracle.connect(conn_str)


def connect_to_db(host: str, username: str, password: str, database: str):
    conn_str = '{}/{}@{}/{}'.format(username, password, host, database)
    conn_creator = partial(get_oracle_connection, conn_str=conn_str)
    engine = create_engine('oracle+cx_oracle://', creator=conn_creator)

    connection = engine.connect()
    print('Successfully connected to {}'.format(conn_str))
    connection.close()

In this case conn_str would be user/pass@host/database_name.

In my Docker container I'm running Python 3 and connecting to the VPN using openconnect. I have also installed the Oracle instantclient in my container.

requirements.txt:

SQLAlchemy==1.2.2
pymssql==2.1.3
cx-Oracle==6.1
psycopg2==2.7.4

Am I making a mistake somewhere here or is this a bug with SQLAlchemy?

Tinmar
  • 31
  • 1
  • 5

1 Answers1

0

The failing code has '{}{}:{}@{}/{}' but the working code has '{}/{}@{}/{}' The former is missing a slash between the username and password, and it's not clear where the port number (after the colon) is set.

Christopher Jones
  • 9,449
  • 3
  • 24
  • 48
  • I don't think that's it. The former string is the database url SQLAlchemy normally takes, which is of the format `dialect[+driver]://user:password@host/dbname`. See the [documentation](http://docs.sqlalchemy.org/en/latest/core/engines.html#sqlalchemy.create_engine) for more info. That is the heart of my problem. I want to use that string format and not special case for an Oracle connection. – Tinmar Feb 14 '18 at 10:31
  • What does db_uri expand to? – Christopher Jones Feb 15 '18 at 00:44
  • `mssql+pymssql://user:pass@host:port_num/database_name` and `oracle+cx_oracle://user:pass@host:port_num/database_name` depending on the database used. I have a feeling it's sqlalchemy not converting the db_uri in the right way for Oracle to understand it. – Tinmar Feb 15 '18 at 11:10
  • What are 'actual' values for those things? And does it need to be user/pass to match your 'working' example? – Christopher Jones Feb 16 '18 at 01:15
  • So the Oracle one would look like this for example: `oracle+cx_oracle://MyUsername:P455w0Rd@ahostname_accessible_through_vpn/thedb`. – Tinmar Feb 16 '18 at 10:01
  • I'm not sure I fully understand your second question. My problem isn't only that the connection strings are different but that to get it to work currently I need to use the cx_Oracle library to establish the connection, and pass a callable with that into `create_engine`. `create_engine` should work by passing in an RFC 1738 URL as specified in the [documentation](http://docs.sqlalchemy.org/en/latest/core/engines.html#oracle), and create the connection for you. – Tinmar Feb 16 '18 at 10:21