baztian / jaydebeapi

JayDeBeApi module allows you to connect from Python code to databases using Java JDBC. It provides a Python DB-API v2.0 to that database.
GNU Lesser General Public License v3.0
365 stars 148 forks source link

Keeps running after SQLException is raised #221

Closed ghost closed 2 years ago

ghost commented 2 years ago

When running the code below I'm presented with a popup asking for my username and password.

Screenshot 2022-09-05 at 17 33 50

If I press cancel, then an SQLException is raised but the script never exits. Also the print statement after jaydebeapi.connect never runs. I tried this on Windows 10 with Java 8 and on Ubuntu with Java 8, 11 and 17 using JayDeBeApi 1.2.3 (and JPype1 1.4.0). What is the reason the script won't exit and how can I make it shutdown properly?

Putting a try/except block around jaydebeapi.connect allows the print statement to run, but the script still doesn't exit.

import jaydebeapi
import urllib.request
from pathlib import Path

jt400_version = "11.0"
jt400_jar_filename = f"jt400-{jt400_version}.jar"
jt400_jar_url = f"https://repo1.maven.org/maven2/net/sf/jt400/jt400/{jt400_version}/{jt400_jar_filename}"

if Path(jt400_jar_filename).is_file():
    print(f"Already exists: {jt400_jar_filename}")
else:
    urllib.request.urlretrieve(jt400_jar_url, jt400_jar_filename)

user = ""
password = ""

jaydebeapi.connect("com.ibm.as400.access.AS400JDBCDriver",
                                f"jdbc:as400://localhost",
                                [user, password],
                                jt400_jar_filename)

print("After cancel (this never prints)")

Logged errors:

Traceback (most recent call last):
  File "org.jpype.JPypeContext.java", line -1, in org.jpype.JPypeContext.callMethod
  File "Method.java", line 498, in java.lang.reflect.Method.invoke
  File "DelegatingMethodAccessorImpl.java", line 43, in sun.reflect.DelegatingMethodAccessorImpl.invoke
  File "NativeMethodAccessorImpl.java", line 62, in sun.reflect.NativeMethodAccessorImpl.invoke
  File "NativeMethodAccessorImpl.java", line -2, in sun.reflect.NativeMethodAccessorImpl.invoke0
  File "DriverManager.java", line 247, in java.sql.DriverManager.getConnection
  File "DriverManager.java", line 664, in java.sql.DriverManager.getConnection
  File "AS400JDBCDriver.java", line 403, in com.ibm.as400.access.AS400JDBCDriver.connect
  File "AS400JDBCDriver.java", line 1280, in com.ibm.as400.access.AS400JDBCDriver.initializeConnection
  File "AS400JDBCDriver.java", line 1446, in com.ibm.as400.access.AS400JDBCDriver.prepareConnection
  File "AS400JDBCConnectionImpl.java", line 3513, in com.ibm.as400.access.AS400JDBCConnectionImpl.setProperties
  File "AS400.java", line 1371, in com.ibm.as400.access.AS400.connectService
  File "AS400.java", line 1414, in com.ibm.as400.access.AS400.connectService
  File "AS400.java", line 4375, in com.ibm.as400.access.AS400.signon
  File "AS400.java", line 3040, in com.ibm.as400.access.AS400.promptSignon
com.ibm.as400.access.AS400SecurityException: com.ibm.as400.access.AS400SecurityException: Signon was canceled.

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

Traceback (most recent call last):
  File "org.jpype.JPypeContext.java", line -1, in org.jpype.JPypeContext.callMethod
  File "Method.java", line 498, in java.lang.reflect.Method.invoke
  File "DelegatingMethodAccessorImpl.java", line 43, in sun.reflect.DelegatingMethodAccessorImpl.invoke
  File "NativeMethodAccessorImpl.java", line 62, in sun.reflect.NativeMethodAccessorImpl.invoke
  File "NativeMethodAccessorImpl.java", line -2, in sun.reflect.NativeMethodAccessorImpl.invoke0
  File "DriverManager.java", line 247, in java.sql.DriverManager.getConnection
  File "DriverManager.java", line 664, in java.sql.DriverManager.getConnection
  File "AS400JDBCDriver.java", line 403, in com.ibm.as400.access.AS400JDBCDriver.connect
  File "AS400JDBCDriver.java", line 1280, in com.ibm.as400.access.AS400JDBCDriver.initializeConnection
  File "AS400JDBCDriver.java", line 1446, in com.ibm.as400.access.AS400JDBCDriver.prepareConnection
  File "AS400JDBCConnectionImpl.java", line 3519, in com.ibm.as400.access.AS400JDBCConnectionImpl.setProperties
  File "JDError.java", line 568, in com.ibm.as400.access.JDError.throwSQLException
  File "JDError.java", line 610, in com.ibm.as400.access.JDError.throwSQLException
  File "JDError.java", line 887, in com.ibm.as400.access.JDError.createSQLExceptionSubClass
Exception: Java Exception

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

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    jaydebeapi.connect("com.ibm.as400.access.AS400JDBCDriver",
  File "/home/user/.local/lib/python3.8/site-packages/jaydebeapi/__init__.py", line 412, in connect
    jconn = _jdbc_connect(jclassname, url, driver_args, jars, libs)
  File "/home/user/.local/lib/python3.8/site-packages/jaydebeapi/__init__.py", line 230, in _jdbc_connect_jpype
    return jpype.java.sql.DriverManager.getConnection(url, *dargs)
java.sql.SQLException: java.sql.SQLException: The application server rejected the connection. (Signon was canceled.)
ghost commented 2 years ago

I have the same issue when using jpype directly: https://github.com/jpype-project/jpype/issues/1088.

ghost commented 2 years ago

This issue is caused by the AS400 driver, not by jaydebeapi. Solution is in in the jpype issue.