Use with clause when acquiring connections from a pool so that they are
returned automatically at the end of the block.
This commit is contained in:
parent
9377a9a0ef
commit
c513b71fe7
|
@ -60,13 +60,12 @@ def callback(message):
|
|||
pool = cx_Oracle.SessionPool(SampleEnv.GetMainUser(),
|
||||
SampleEnv.GetMainPassword(), SampleEnv.GetConnectString(), min=2,
|
||||
max=5, increment=1, events=True, threaded=True)
|
||||
connection = pool.acquire()
|
||||
sub = connection.subscribe(callback=callback, timeout=1800,
|
||||
qos=cx_Oracle.SUBSCR_QOS_QUERY | cx_Oracle.SUBSCR_QOS_ROWIDS)
|
||||
print("Subscription created with ID:", sub.id)
|
||||
queryId = sub.registerquery("select * from TestTempTable")
|
||||
print("Registered query with ID:", queryId)
|
||||
connection.close()
|
||||
with pool.acquire() as connection:
|
||||
sub = connection.subscribe(callback=callback, timeout=1800,
|
||||
qos=cx_Oracle.SUBSCR_QOS_QUERY | cx_Oracle.SUBSCR_QOS_ROWIDS)
|
||||
print("Subscription created with ID:", sub.id)
|
||||
queryId = sub.registerquery("select * from TestTempTable")
|
||||
print("Registered query with ID:", queryId)
|
||||
|
||||
while registered:
|
||||
print("Waiting for notifications....")
|
||||
|
|
|
@ -81,46 +81,42 @@ pool = cx_Oracle.SessionPool(SampleEnv.GetMainUser(),
|
|||
# newly created, the callback will be invoked but since there is no tag
|
||||
# specified, no session state will be changed
|
||||
print("(1) acquire session without tag")
|
||||
conn = pool.acquire()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
conn.close()
|
||||
with pool.acquire() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
|
||||
# acquire session, specifying a tag; since the session returned has no tag,
|
||||
# the callback will be invoked; session state will be changed and the tag will
|
||||
# be saved when the connection is closed
|
||||
print("(2) acquire session with tag")
|
||||
conn = pool.acquire(tag="NLS_DATE_FORMAT=SIMPLE")
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
conn.close()
|
||||
with pool.acquire(tag="NLS_DATE_FORMAT=SIMPLE") as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
|
||||
# acquire session, specifying the same tag; since a session exists in the pool
|
||||
# with this tag, it will be returned and the callback will not be invoked but
|
||||
# the connection will still have the session state defined previously
|
||||
print("(3) acquire session with same tag")
|
||||
conn = pool.acquire(tag="NLS_DATE_FORMAT=SIMPLE")
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
conn.close()
|
||||
with pool.acquire(tag="NLS_DATE_FORMAT=SIMPLE") as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
|
||||
# acquire session, specifying a different tag; since no session exists in the
|
||||
# pool with this tag, a new session will be returned and the callback will be
|
||||
# invoked; session state will be changed and the tag will be saved when the
|
||||
# connection is closed
|
||||
print("(4) acquire session with different tag")
|
||||
conn = pool.acquire(tag="NLS_DATE_FORMAT=FULL;TIME_ZONE=UTC")
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
conn.close()
|
||||
with pool.acquire(tag="NLS_DATE_FORMAT=FULL;TIME_ZONE=UTC") as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
|
||||
# acquire session, specifying a different tag but also specifying that a
|
||||
# session with any tag can be acquired from the pool; a session with one of the
|
||||
|
@ -128,10 +124,9 @@ conn.close()
|
|||
# session state will be changed and the tag will be saved when the connection
|
||||
# is closed
|
||||
print("(4) acquire session with different tag but match any also specified")
|
||||
conn = pool.acquire(tag="NLS_DATE_FORMAT=FULL;TIME_ZONE=MST", matchanytag=True)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
conn.close()
|
||||
|
||||
with pool.acquire(tag="NLS_DATE_FORMAT=FULL;TIME_ZONE=MST", matchanytag=True) \
|
||||
as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
|
|
|
@ -28,54 +28,49 @@ pool = cx_Oracle.SessionPool(SampleEnv.GetMainUser(),
|
|||
sessionCallback="pkg_SessionCallback.TheCallback")
|
||||
|
||||
# truncate table logging calls to PL/SQL session callback
|
||||
conn = pool.acquire()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("truncate table PLSQLSessionCallbacks")
|
||||
conn.close()
|
||||
with pool.acquire() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("truncate table PLSQLSessionCallbacks")
|
||||
|
||||
# acquire session without specifying a tag; the callback will not be invoked as
|
||||
# a result and no session state will be changed
|
||||
print("(1) acquire session without tag")
|
||||
conn = pool.acquire()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
conn.close()
|
||||
with pool.acquire() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
|
||||
# acquire session, specifying a tag; since the session returned has no tag,
|
||||
# the callback will be invoked; session state will be changed and the tag will
|
||||
# be saved when the connection is closed
|
||||
print("(2) acquire session with tag")
|
||||
conn = pool.acquire(tag="NLS_DATE_FORMAT=SIMPLE")
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
conn.close()
|
||||
with pool.acquire(tag="NLS_DATE_FORMAT=SIMPLE") as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
|
||||
# acquire session, specifying the same tag; since a session exists in the pool
|
||||
# with this tag, it will be returned and the callback will not be invoked but
|
||||
# the connection will still have the session state defined previously
|
||||
print("(3) acquire session with same tag")
|
||||
conn = pool.acquire(tag="NLS_DATE_FORMAT=SIMPLE")
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
conn.close()
|
||||
with pool.acquire(tag="NLS_DATE_FORMAT=SIMPLE") as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
|
||||
# acquire session, specifying a different tag; since no session exists in the
|
||||
# pool with this tag, a new session will be returned and the callback will be
|
||||
# invoked; session state will be changed and the tag will be saved when the
|
||||
# connection is closed
|
||||
print("(4) acquire session with different tag")
|
||||
conn = pool.acquire(tag="NLS_DATE_FORMAT=FULL;TIME_ZONE=UTC")
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
conn.close()
|
||||
with pool.acquire(tag="NLS_DATE_FORMAT=FULL;TIME_ZONE=UTC") as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
|
||||
# acquire session, specifying a different tag but also specifying that a
|
||||
# session with any tag can be acquired from the pool; a session with one of the
|
||||
|
@ -83,21 +78,20 @@ conn.close()
|
|||
# session state will be changed and the tag will be saved when the connection
|
||||
# is closed
|
||||
print("(4) acquire session with different tag but match any also specified")
|
||||
conn = pool.acquire(tag="NLS_DATE_FORMAT=FULL;TIME_ZONE=MST", matchanytag=True)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
conn.close()
|
||||
with pool.acquire(tag="NLS_DATE_FORMAT=FULL;TIME_ZONE=MST", matchanytag=True) \
|
||||
as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("select to_char(current_date) from dual")
|
||||
result, = cursor.fetchone()
|
||||
print("main(): result is", repr(result))
|
||||
|
||||
# acquire session and display results from PL/SQL session logs
|
||||
conn = pool.acquire()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
select RequestedTag, ActualTag
|
||||
from PLSQLSessionCallbacks
|
||||
order by FixupTimestamp""")
|
||||
print("(5) PL/SQL session callbacks")
|
||||
for requestedTag, actualTag in cursor:
|
||||
print("Requested:", requestedTag, "Actual:", actualTag)
|
||||
|
||||
with pool.acquire() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
select RequestedTag, ActualTag
|
||||
from PLSQLSessionCallbacks
|
||||
order by FixupTimestamp""")
|
||||
print("(5) PL/SQL session callbacks")
|
||||
for requestedTag, actualTag in cursor:
|
||||
print("Requested:", requestedTag, "Actual:", actualTag)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#------------------------------------------------------------------------------
|
||||
# Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
# Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
#------------------------------------------------------------------------------
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
|
@ -22,41 +22,40 @@ pool = cx_Oracle.SessionPool(SampleEnv.GetMainUser(),
|
|||
max=5, increment=1, threaded=True)
|
||||
|
||||
# dbms_session.sleep() replaces dbms_lock.sleep() from Oracle Database 18c
|
||||
conn = pool.acquire()
|
||||
sleepProcName = "dbms_session.sleep" \
|
||||
if int(conn.version.split(".")[0]) >= 18 \
|
||||
else "dbms_lock.sleep"
|
||||
conn.close()
|
||||
with pool.acquire() as conn:
|
||||
sleepProcName = "dbms_session.sleep" \
|
||||
if int(conn.version.split(".")[0]) >= 18 \
|
||||
else "dbms_lock.sleep"
|
||||
|
||||
def TheLongQuery():
|
||||
conn = pool.acquire()
|
||||
cursor = conn.cursor()
|
||||
cursor.arraysize = 25000
|
||||
print("TheLongQuery(): beginning execute...")
|
||||
cursor.execute("""
|
||||
select *
|
||||
from
|
||||
TestNumbers
|
||||
cross join TestNumbers
|
||||
cross join TestNumbers
|
||||
cross join TestNumbers
|
||||
cross join TestNumbers
|
||||
cross join TestNumbers""")
|
||||
print("TheLongQuery(): done execute...")
|
||||
while True:
|
||||
rows = cursor.fetchmany()
|
||||
if not rows:
|
||||
break
|
||||
print("TheLongQuery(): fetched", len(rows), "rows...")
|
||||
print("TheLongQuery(): all done!")
|
||||
with pool.acquire() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.arraysize = 25000
|
||||
print("TheLongQuery(): beginning execute...")
|
||||
cursor.execute("""
|
||||
select *
|
||||
from
|
||||
TestNumbers
|
||||
cross join TestNumbers
|
||||
cross join TestNumbers
|
||||
cross join TestNumbers
|
||||
cross join TestNumbers
|
||||
cross join TestNumbers""")
|
||||
print("TheLongQuery(): done execute...")
|
||||
while True:
|
||||
rows = cursor.fetchmany()
|
||||
if not rows:
|
||||
break
|
||||
print("TheLongQuery(): fetched", len(rows), "rows...")
|
||||
print("TheLongQuery(): all done!")
|
||||
|
||||
|
||||
def DoALock():
|
||||
conn = pool.acquire()
|
||||
cursor = conn.cursor()
|
||||
print("DoALock(): beginning execute...")
|
||||
cursor.callproc(sleepProcName, (5,))
|
||||
print("DoALock(): done execute...")
|
||||
with pool.acquire() as conn:
|
||||
cursor = conn.cursor()
|
||||
print("DoALock(): beginning execute...")
|
||||
cursor.callproc(sleepProcName, (5,))
|
||||
print("DoALock(): done execute...")
|
||||
|
||||
|
||||
thread1 = threading.Thread(None, TheLongQuery)
|
||||
|
@ -69,4 +68,3 @@ thread1.join()
|
|||
thread2.join()
|
||||
|
||||
print("All done!")
|
||||
|
||||
|
|
Loading…
Reference in New Issue