ibmdb / python-ibmdb-django

IBM DB2 Driver for the Django application Framework
Apache License 2.0
29 stars 42 forks source link

Issue connecting to DB2 11.05.0800 #85

Closed rajddesai closed 1 year ago

rajddesai commented 1 year ago

I'm getting the following error when trying to connect to DB2 with settings (tried with both pconnect true/false): Settings:

DATABASES = {
    'default': {
        'ENGINE': 'ibm_db_django',
        'NAME': 'MAX',
        'USER': 'admin',
        'PASSWORD': os.environ.get("DB2_PASSWORD"),
        'HOST': 'cpd-analytics.apps.analytics.cp.fyre.ibm.com',
        'POST': '30921',
        'PCONNECT': True
    },
    'original': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

Error:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/ibm_db_dbi.py", line 679, in pconnect
    conn = ibm_db.pconnect(dsn, '', '', conn_options)
Exception: [IBM][CLI Driver] SQL30081N  A communication error has been detected. Communication protocol being used: "TCP/IP".  Communication API being used: "SOCKETS".  Location where the error was detected: "9.30.235.124".  Communication function detecting the error: "recv".  Protocol specific error code(s): "*", "*", "0".  SQLSTATE=08001 SQLCODE=-30081

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.8/site-packages/ibm_db_django/base.py", line 341, in get_new_connection
    connection = self.databaseWrapper.get_new_connection(conn_params)
  File "/usr/local/lib/python3.8/site-packages/ibm_db_django/pybase.py", line 123, in get_new_connection
    connection = Database.pconnect( **kwargs )
  File "/usr/local/lib/python3.8/site-packages/ibm_db_dbi.py", line 684, in pconnect
    raise _get_exception(inst)
ibm_db_dbi.OperationalError: ibm_db_dbi::OperationalError: [IBM][CLI Driver] SQL30081N  A communication error has been detected. Communication protocol being used: "TCP/IP".  Communication API being used: "SOCKETS".  Location where the error was detected: "9.30.235.124".  Communication function detecting the error: "recv".  Protocol specific error code(s): "*", "*", "0".  SQLSTATE=08001 SQLCODE=-30081

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

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
    self.check_migrations()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 486, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 220, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
    self.ensure_connection()
  File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.8/site-packages/ibm_db_django/base.py", line 341, in get_new_connection
    connection = self.databaseWrapper.get_new_connection(conn_params)
  File "/usr/local/lib/python3.8/site-packages/ibm_db_django/pybase.py", line 123, in get_new_connection
    connection = Database.pconnect( **kwargs )
  File "/usr/local/lib/python3.8/site-packages/ibm_db_dbi.py", line 684, in pconnect
    raise _get_exception(inst)
django.db.utils.OperationalError: [IBM][CLI Driver] SQL30081N  A communication error has been detected. Communication protocol being used: "TCP/IP".  Communication API being used: "SOCKETS".  Location where the error was detected: "9.30.235.124".  Communication function detecting the error: "recv".  Protocol specific error code(s): "*", "*", "0".  SQLSTATE=08001 SQLCODE=-30081

Using the same connection information I can connect using the ibm_db package so I'm not sure what is going on here. Code:

        try:
            db2_pass = os.environ.get("DB2_PASSWORD")
            conn = ibm_db.connect(f"DATABASE=MAX;HOSTNAME=cpd-analytics.apps.analytics.cp.fyre.ibm.com;PORT=30921;PROTOCOL=TCPIP;UID=admin;PWD={db2_pass};", "", "")
            server = ibm_db.server_info(conn)
            print(f"DBMS_NAME: {server.DBMS_NAME}")
            print(f"DBMS_VER: {server.DBMS_VER}")
            print(f"DB_NAME: {server.DB_NAME}")
            stmt = ibm_db.exec_immediate(conn, "select * from DB2INST1.EMPLOYEE")
            while ibm_db.fetch_row(stmt) != False:
                print(f"ID {ibm_db.result(stmt, 0)}, Name {ibm_db.result(stmt, 1)}")
        except Exception as e:
            print(f"Error: {e}")

Output:

root@c485145acd47:/maxoptics# python manage.py connect_db2
DBMS_NAME: DB2/LINUXX8664
DBMS_VER: 11.05.0800
DB_NAME: MAX
ID 1, Name Frank
praveen-db2 commented 1 year ago

@rajddesai its PORT nor POST, Correct and check

DATABASES = { 'default': { 'ENGINE': 'ibm_db_django', 'NAME': 'MAX', 'USER': 'admin', 'PASSWORD': os.environ.get("DB2_PASSWORD"), 'HOST': 'cpd-analytics.apps.analytics.cp.fyre.ibm.com', 'POST': '30921', 'PCONNECT': True }, 'original': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }

rajddesai commented 1 year ago

Ah 🤦 thanks for spotting this, sorry for this works fine after fixing.