Python 我应该如何使用jaydebeapi和jpype处理oracle数据库连接类
我创建了一个类来管理我的连接,如下所示:Python 我应该如何使用jaydebeapi和jpype处理oracle数据库连接类,python,oracle,jpype,jaydebeapi,Python,Oracle,Jpype,Jaydebeapi,我创建了一个类来管理我的连接,如下所示: class DB(object): def __init__(self): JHOME = jpype.getDefaultJVMPath() # ojdbc path should be added to classpath jpype.startJVM(JHOME, r'-Djava.class.path=..\ojdbc8-full\ojdbc8.jar') # Might need error handling h
class DB(object):
def __init__(self):
JHOME = jpype.getDefaultJVMPath()
# ojdbc path should be added to classpath
jpype.startJVM(JHOME, r'-Djava.class.path=..\ojdbc8-full\ojdbc8.jar')
# Might need error handling here
self.conn = jaydebeapi.connect(
cfg.oracle['driver'],
cfg.oracle['url'],
[cfg.oracle['user'], cfg.oracle['pass']])
# destructor
def __del__(self):
self.conn.close()
print('connection destroyed')
def query(self, sql):
with self.conn.cursor() as cursor:
cursor.execute(sql)
return cursor.fetchall()
我全局实例化了它一次,并让一些函数调用查询方法。这看起来一切都很好,但我有一个函数调用循环中使用查询方法的函数。所有查询都已成功完成,但它显示错误:
Exception ignored in: <function DB.__del__ at 0x00000000029ABB88>
Traceback (most recent call last):
File "file.py", line 33, in `__del__`
File "C:\Users\path\to\project\packages\jaydebeapi\__init__.py", line 437, in close
jpype._core.JVMNotRunning: Java Virtual Machine is not running
中忽略的异常:
回溯(最近一次呼叫最后一次):
文件“File.py”,第33行,在`\u del中__`
文件“C:\Users\path\to\project\packages\jaydebeapi\\uuuu init\uuuu.py”,第437行,关闭
jpype._core.JVMNotRunning:Java虚拟机未运行
我认为这个错误是在垃圾收集过程中抛出的。在处理这个数据库连接类时,我做错了什么?我认为conn对象是垃圾收集并关闭的,而DB对象是垃圾收集并关闭的。我拆下了线路,一切似乎都正常。希望那个连接已经关闭了!