我正在尝试使用 JayDeBeApi 包连接到 Python 3 中的 Teradata 数据库。这是使用 Flask 和 Flask-Restplus
的API项目的一部分这是一个重现问题的最小工作示例。 在终端窗口中,键入以下命令来设置工作站:
# Install JVM
sudo apt-get install default-jre
# Create Python virtual environment
sudo apt-get install python3-venv
python3 -m venv jdbc
source jdbc/bin/activate
# Install Python packages in virtual environment
pip3 install --upgrade pip
pip3 install jaydebeapi
pip3 install flask
使用以下内容创建文件app.py:
from flask import Flask
import jaydebeapi
app = Flask(__name__)
def get_jdbc_connection():
connection = jaydebeapi.connect(
'com.teradata.jdbc.TeraDriver',
'jdbc:teradata://edw-dev.company.org',
{'user': 'LOGIN', 'password': 'PASSWORD', 'tmode': 'TERA', 'charset': 'UTF8'},
'/home/alexis/teradataDriverJdbc.jar')
return connection
@app.route('/hello/')
def hello_world():
print('Init second connection')
get_jdbc_connection()
print('Success')
return 'Hello world!'
if __name__ == '__main__':
print('Init connection')
test_connection = get_jdbc_connection()
test_connection.close()
print('Init connection closed')
app.run(host='localhost', port=5000, threaded=True, debug=True)
在激活虚拟环境的终端窗口中,键入以下内容:
python3
>>> import app
>>> app.get_jdbc_connection()
<jaydebeapi.Connection object at 0x7f28ecb2ebe0>
上面的代码段工作,方法get_jdbc_connection()
返回一个连接对象。
现在尝试使用Flask运行它。在激活虚拟环境的终端窗口中,键入以下内容:
python3 app.py
Init connection
Init connection closed
* Running on http://localhost:5000/ (Press CTRL+C to quit)
* Restarting with stat
Init connection
Init connection closed
* Debugger is active!
* Debugger PIN: 301-429-987
在浏览器中,导航至http://localhost:5000/hello/ 您将收到以下错误消息,该消息与我在上面的第一条消息中粘贴的消息相同:
Init second connection
127.0.0.1 - - [08/Mar/2018 16:00:30] "GET /hello/ HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/alexis/dq/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/alexis/dq/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/home/alexis/dq/lib/python3.5/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/alexis/app.py", line 19, in hello_world
get_jdbc_connection()
File "/home/alexis/app.py", line 12, in get_jdbc_connection
'/home/alexis/teradataDriverJdbc.jar')
File "/home/alexis/dq/lib/python3.5/site-packages/jaydebeapi/__init__.py", line 381, in connect
jconn = _jdbc_connect(jclassname, url, driver_args, jars, libs)
File "/home/alexis/dq/lib/python3.5/site-packages/jaydebeapi/__init__.py", line 199, in _jdbc_connect_jpype
return jpype.java.sql.DriverManager.getConnection(url, *dargs)
jpype._jexception.java.sql.SQLExceptionPyRaisable: java.sql.SQLException: No suitable driver found for jdbc:teradata://edw-dev.company.org
备注:我还在Github项目中创建了一个问题:https://github.com/baztian/jaydebeapi/issues/66
编辑:根据baztian的建议,我在jPype Github项目上提出了问题:https://github.com/originell/jpype/issues/290
答案 0 :(得分:2)
以下解决方法可以解决您的问题:
def get_jdbc_connection():
import jpype
if jpype.isJVMStarted() and not jpype.isThreadAttachedToJVM():
jpype.attachThreadToJVM()
jpype.java.lang.Thread.currentThread().setContextClassLoader(jpype.java.lang.ClassLoader.getSystemClassLoader())
connection = jaydebeapi.connect(
'com.teradata.jdbc.TeraDriver',
'jdbc:teradata://edw-dev.company.org',
{'user': 'LOGIN', 'password': 'PASSWORD', 'tmode': 'TERA', 'charset': 'UTF8'},
'/home/alexis/teradataDriverJdbc.jar')
return connection
答案 1 :(得分:0)
这可能是一个jpype问题。 Jpype是JayDeBeApi的基础。 github上有一些开放的jpype问题。请确保您使用的是lates jpype版本。否则你必须为jaydebeapi提供PR或希望有人为你修复它。 还可以考虑将Jython与JayDeBeApi一起使用。
编辑:还要考虑使用专用的python信号量,它使用JayDeBeApi封装对模块的访问。