|
| 1 | +#------------------------------------------------------------------------------ |
| 2 | +# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
| 3 | +#------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +#------------------------------------------------------------------------------ |
| 6 | +# SessionCallback.py |
| 7 | +# |
| 8 | +# Demonstrate how to use a session callback written in Python. The callback is |
| 9 | +# invoked whenever a newly created session is acquired from the pool, or when |
| 10 | +# the requested tag does not match the tag that is associated with the |
| 11 | +# session. It is generally used to set session state, so that the application |
| 12 | +# can count on known session state, which allows the application to reduce the |
| 13 | +# number of round trips made to the database. |
| 14 | +# |
| 15 | +# This script requires cx_Oracle 7.1 and higher. |
| 16 | +#------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +from __future__ import print_function |
| 19 | + |
| 20 | +import cx_Oracle |
| 21 | +import SampleEnv |
| 22 | + |
| 23 | +# define a dictionary of NLS_DATE_FORMAT formats supported by this sample |
| 24 | +SUPPORTED_FORMATS = { |
| 25 | + "SIMPLE" : "'YYYY-MM-DD HH24:MI'", |
| 26 | + "FULL" : "'YYYY-MM-DD HH24:MI:SS'" |
| 27 | +} |
| 28 | + |
| 29 | +# define a dictionary of TIME_ZONE values supported by this sample |
| 30 | +SUPPORTED_TIME_ZONES = { |
| 31 | + "UTC" : "'UTC'", |
| 32 | + "MST" : "'-07:00'" |
| 33 | +} |
| 34 | + |
| 35 | +# define a dictionary of keys that are supported by this sample |
| 36 | +SUPPORTED_KEYS = { |
| 37 | + "NLS_DATE_FORMAT" : SUPPORTED_FORMATS, |
| 38 | + "TIME_ZONE" : SUPPORTED_TIME_ZONES |
| 39 | +} |
| 40 | + |
| 41 | +# define session callback |
| 42 | +def init_session(conn, requestedTag): |
| 43 | + |
| 44 | + # display the requested and actual tags |
| 45 | + print("init_session(): requested tag=%r, actual tag=%r" % \ |
| 46 | + (requestedTag, conn.tag)) |
| 47 | + |
| 48 | + # tags are expected to be in the form "key1=value1;key2=value2" |
| 49 | + # in this example, they are used to set NLS parameters and the tag is |
| 50 | + # parsed to validate it |
| 51 | + if requestedTag is not None: |
| 52 | + stateParts = [] |
| 53 | + for directive in requestedTag.split(";"): |
| 54 | + parts = directive.split("=") |
| 55 | + if len(parts) != 2: |
| 56 | + raise ValueError("Tag must contain key=value pairs") |
| 57 | + key, value = parts |
| 58 | + valueDict = SUPPORTED_KEYS.get(key) |
| 59 | + if valueDict is None: |
| 60 | + raise ValueError("Tag only supports keys: %s" % \ |
| 61 | + (", ".join(SUPPORTED_KEYS))) |
| 62 | + actualValue = valueDict.get(value) |
| 63 | + if actualValue is None: |
| 64 | + raise ValueError("Key %s only supports values: %s" % \ |
| 65 | + (key, ", ".join(valueDict))) |
| 66 | + stateParts.append("%s = %s" % (key, actualValue)) |
| 67 | + sql = "alter session set %s" % " ".join(stateParts) |
| 68 | + cursor = conn.cursor() |
| 69 | + cursor.execute(sql) |
| 70 | + |
| 71 | + # assign the requested tag to the connection so that when the connection |
| 72 | + # is closed, it will automatically be retagged; note that if the requested |
| 73 | + # tag is None (no tag was requested) this has no effect |
| 74 | + conn.tag = requestedTag |
| 75 | + |
| 76 | + |
| 77 | +# create pool with session callback defined |
| 78 | +pool = cx_Oracle.SessionPool(SampleEnv.MAIN_USER, SampleEnv.MAIN_PASSWORD, |
| 79 | + SampleEnv.CONNECT_STRING, min=2, max=5, increment=1, threaded=True, |
| 80 | + sessionCallback=init_session) |
| 81 | + |
| 82 | +# acquire session without specifying a tag; since the session returned is |
| 83 | +# newly created, the callback will be invoked but since there is no tag |
| 84 | +# specified, no session state will be changed |
| 85 | +print("(1) acquire session without tag") |
| 86 | +conn = pool.acquire() |
| 87 | +cursor = conn.cursor() |
| 88 | +cursor.execute("select to_char(current_date) from dual") |
| 89 | +result, = cursor.fetchone() |
| 90 | +print("main(): result is", repr(result)) |
| 91 | +conn.close() |
| 92 | + |
| 93 | +# acquire session, specifying a tag; since the session returned has no tag, |
| 94 | +# the callback will be invoked; session state will be changed and the tag will |
| 95 | +# be saved when the connection is closed |
| 96 | +print("(2) acquire session with tag") |
| 97 | +conn = pool.acquire(tag="NLS_DATE_FORMAT=SIMPLE") |
| 98 | +cursor = conn.cursor() |
| 99 | +cursor.execute("select to_char(current_date) from dual") |
| 100 | +result, = cursor.fetchone() |
| 101 | +print("main(): result is", repr(result)) |
| 102 | +conn.close() |
| 103 | + |
| 104 | +# acquire session, specifying the same tag; since a session exists in the pool |
| 105 | +# with this tag, it will be returned and the callback will not be invoked but |
| 106 | +# the connection will still have the session state defined previously |
| 107 | +print("(3) acquire session with same tag") |
| 108 | +conn = pool.acquire(tag="NLS_DATE_FORMAT=SIMPLE") |
| 109 | +cursor = conn.cursor() |
| 110 | +cursor.execute("select to_char(current_date) from dual") |
| 111 | +result, = cursor.fetchone() |
| 112 | +print("main(): result is", repr(result)) |
| 113 | +conn.close() |
| 114 | + |
| 115 | +# acquire session, specifying a different tag; since no session exists in the |
| 116 | +# pool with this tag, a new session will be returned and the callback will be |
| 117 | +# invoked; session state will be changed and the tag will be saved when the |
| 118 | +# connection is closed |
| 119 | +print("(4) acquire session with different tag") |
| 120 | +conn = pool.acquire(tag="NLS_DATE_FORMAT=FULL;TIME_ZONE=UTC") |
| 121 | +cursor = conn.cursor() |
| 122 | +cursor.execute("select to_char(current_date) from dual") |
| 123 | +result, = cursor.fetchone() |
| 124 | +print("main(): result is", repr(result)) |
| 125 | +conn.close() |
| 126 | + |
| 127 | +# acquire session, specifying a different tag but also specifying that a |
| 128 | +# session with any tag can be acquired from the pool; a session with one of the |
| 129 | +# previously set tags will be returned and the callback will be invoked; |
| 130 | +# session state will be changed and the tag will be saved when the connection |
| 131 | +# is closed |
| 132 | +print("(4) acquire session with different tag but match any also specified") |
| 133 | +conn = pool.acquire(tag="NLS_DATE_FORMAT=FULL;TIME_ZONE=MST", matchanytag=True) |
| 134 | +cursor = conn.cursor() |
| 135 | +cursor.execute("select to_char(current_date) from dual") |
| 136 | +result, = cursor.fetchone() |
| 137 | +print("main(): result is", repr(result)) |
| 138 | +conn.close() |
| 139 | + |
0 commit comments