我很想知道这是否是使用cx_Oracle与上下文lib和使用DBRCP连接池的正确方法。
import cx_Oracle
import threading
import time
def get_connection():
connection = cx_Oracle.connect(user='username', password='password', dsn='mydsn_name/service_name:pooled')
return connection
def myfunc():
with get_connection() as conn:
cursor = conn.cursor()
for _ in range(10):
cursor.execute("select * from mytable")
val = cursor.fetchone()
time.sleep(60)
print("Thread", threading.current_thread().name, "fetched sequence =", val)
results = []
for thread in range(0,10):
current_thread = threading.Thread(name = f'Thread {thread}', target = myfunc)
results.append(current_thread)
current_thread.start()
print('Started All Threads')
for thread in results:
thread.join()
print("All done!")
我不确定我是否在这里做正确的事。
并且不知道如何确认连接正在返回连接池。
每个帖子都没有为数据库打开全新的连接。
虽然cx_Oracle上的文档似乎表明我正走在正确的道路上。
答案 0 :(得分:0)
如果您还在DRCP的同时使用cx_Oracle连接池,那么您将获得最大收益。您需要使用DRCP设置pool = cx_Oracle.SessionPool("pythonhol", "welcome", "localhost/orclpdb:pooled",
min = 2, max = 5, increment = 1, threaded = True)
def Query():
con = pool.acquire(cclass="PYTHONHOL", purity=cx_Oracle.ATTR_PURITY_SELF)
#con = pool.acquire(cclass="PYTHONHOL", purity=cx_Oracle.ATTR_PURITY_NEW)
cur = con.cursor()
for i in range(4):
cur.execute("select myseq.nextval from dual")
seqval, = cur.fetchone()
,否则您将失去其优势。然后,您可以决定使用何种级别的会话重用('纯度')。检查cx_Oracle tutorial。来自solutions/connect_pool2.py:
count
您可以查询V $ CPOOL_STATS等V $视图,以检查是否正在使用DRCP。某些资源的链接位于https://oracle.github.io/node-oracledb/doc/api.html#drcp