oracledb.exceptions.DatabaseError: DPY-4011: the database or network closed the connection - python oracledb library

195 views Asked by At

I'm trying to connect oracle 11g with python

import oracledb
import os

user = 'system'
password = 'admin123'
port = 1521
service_name = 'xe'
oracle_server_addr = 'localhost'

conn_string = "{oracle_server_addr}:{port}/{service_name}".format(oracle_server_addr=oracle_server_addr, port=port, service_name=service_name)

print(conn_string)

with oracledb.connect(user=user, password=password, dsn=conn_string) as conn:
    with conn.cursor() as cursor:
        sql = """select sysdate from dual"""
        for r in cursor.execute(sql):
            print(r)

But I'm getting this error:

conn_string: localhost:1521/xe

Traceback (most recent call last):
  File "src/oracledb/impl/thin/connection.pyx", line 353, in oracledb.thin_impl.ThinConnImpl._connect_with_address
  File "src/oracledb/impl/thin/protocol.pyx", line 207, in oracledb.thin_impl.Protocol._connect_phase_one
  File "src/oracledb/impl/thin/protocol.pyx", line 386, in oracledb.thin_impl.Protocol._process_message
  File "src/oracledb/impl/thin/protocol.pyx", line 365, in oracledb.thin_impl.Protocol._process_message
  File "src/oracledb/impl/thin/messages.pyx", line 1835, in oracledb.thin_impl.ConnectMessage.process
  File "src/oracledb/impl/thin/buffer.pyx", line 845, in oracledb.thin_impl.Buffer.read_uint32
  File "src/oracledb/impl/thin/packet.pyx", line 235, in oracledb.thin_impl.ReadBuffer._get_raw
  File "src/oracledb/impl/thin/packet.pyx", line 588, in oracledb.thin_impl.ReadBuffer.wait_for_packets_sync
  File "src/oracledb/impl/thin/transport.pyx", line 306, in oracledb.thin_impl.Transport.read_packet
  File "/home/acme/.local/lib/python3.10/site-packages/oracledb/errors.py", line 162, in _raise_err
    raise exc_type(_Error(message)) from cause
oracledb.exceptions.DatabaseError: DPY-4011: the database or network closed the connection
Help: https://python-oracledb.readthedocs.io/en/latest/user_guide/troubleshooting.html#dpy-4011

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

Traceback (most recent call last):
  File "/tmp/workspace/sql_ping/sql_ping.py", line 16, in <module>
    with oracledb.connect(user=user, password=password, dsn=conn_string) as conn:
  File "/home/acme/.local/lib/python3.10/site-packages/oracledb/connection.py", line 1134, in connect
    return conn_class(dsn=dsn, pool=pool, params=params, **kwargs)
  File "/home/acme/.local/lib/python3.10/site-packages/oracledb/connection.py", line 523, in __init__
    impl.connect(params_impl)
  File "src/oracledb/impl/thin/connection.pyx", line 449, in oracledb.thin_impl.ThinConnImpl.connect
  File "src/oracledb/impl/thin/connection.pyx", line 445, in oracledb.thin_impl.ThinConnImpl.connect
  File "src/oracledb/impl/thin/connection.pyx", line 411, in oracledb.thin_impl.ThinConnImpl._connect_with_params
  File "src/oracledb/impl/thin/connection.pyx", line 392, in oracledb.thin_impl.ThinConnImpl._connect_with_description
  File "src/oracledb/impl/thin/connection.pyx", line 358, in oracledb.thin_impl.ThinConnImpl._connect_with_address
  File "/home/acme/.local/lib/python3.10/site-packages/oracledb/errors.py", line 162, in _raise_err
    raise exc_type(_Error(message)) from cause
oracledb.exceptions.OperationalError: DPY-6005: cannot connect to database (CONNECTION_ID=emkjRug2341/ymv7uTC0Bg==).
DPY-4011: the database or network closed the connection
Help: https://python-oracledb.readthedocs.io/en/latest/user_guide/troubleshooting.html#dpy-4011

Context (Environment)

Both (database and client app are in the same host)

  • Database

    • Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit (localhost)
    • ubuntu
    • user = 'system'
    • password = 'admin123'
    • port = 1521
    • service_name = 'xe'
    • host = 'localhost' (I also tried with local ip and 127.0.0.1)
  • App

    • ubuntu
    • python 3.10
    • oracledb 2.0.1 & 2.0.0
    • thin mode
  • No firewall, proxy, antivirus, etc. All in my ubuntu localhost

Attempts

# Works with java

In the same host, with the same database and thin mode, java <3 is able to connect without any problems. Code is here

# Works with dbeaver database ide

enter image description here

# oracledb pip library downgrade

I tried with all the versions since 1.3.1 and the error is almost the same

https://pypi.org/project/oracledb/#history

1.2.1, 1.3.2, 1.4.2

oracledb.exceptions.NotSupportedError: DPY-3010: connections to this database server version are not supported by python-oracledb in thin mode

# disable_oob= True

I tried this answer but the error is the same

# Similar unsolved questions

1

There are 1 answers

2
MT0 On

You cannot using thin mode as OracleDB does not support Oracle 11.

From the Python OracleDB PyPi page:

Dependencies and Interoperability

  • Python versions 3.7 through 3.12.

    Prebuilt packages are available for these Python versions on Windows, on macOS and on Linux.

    Source code is also available.

  • Oracle Client libraries are optional.

    Thin mode: By default python-oracledb runs in a 'Thin' mode which connects directly to Oracle Database.

    Thick mode: Some advanced Oracle Database functionality is currently only available when optional Oracle Client libraries are loaded by python-oracledb. Libraries are available in the free Oracle Instant Client packages. Python-oracledb can use Oracle Client libraries 11.2 through 21c.

  • Oracle Database

    Thin mode: Oracle Database 12.1 (or later) is required.

    Thick mode: Oracle Database 11.2 (or later) is required, depending on the Oracle Client library version. Oracle Database's standard client-server version interoperability allows connection to both older and newer databases. For example when python-oracledb uses Oracle Client 19c libraries, then it can connect to Oracle Database 11.2 or later.

Either use thick mode or upgrade your database to at least Oracle 12.1.