Adjust samples so that they run unchanged against Oracle Cloud databa… · leonbu/python-cx_Oracle@952580a · GitHub
Skip to content

Commit 952580a

Browse files
Adjust samples so that they run unchanged against Oracle Cloud databases:
- administrative user is ADMIN on the Oracle Cloud databases and SYSTEM on local databases (SYSDBA is not available on the Oracle Cloud database) - use dbms_session.sleep() instead of dbms_lock.sleep() where possible - environment variables CX_ORACLE_SAMPLES_SYSDBA_USER and CX_ORACLE_SAMPLES_SYSDBA_PASSWORD are replaced with CX_ORACLE_SAMPLES_ADMIN_USER and CX_ORACLE_SAMPLES_ADMIN_PASSWORD - new environment variable CX_ORACLE_SAMPLES_DRCP_CONNECT_STRING used for specifying the connect string to use for DRCP usage
1 parent d16c939 commit 952580a

6 files changed

Lines changed: 53 additions & 22 deletions

File tree

samples/CallTimeout.py

Lines changed: 6 additions & 1 deletion

samples/DropSamples.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ def DropSamples(conn):
2121
edition_name = SampleEnv.GetEditionName())
2222

2323
if __name__ == "__main__":
24-
conn = cx_Oracle.connect(SampleEnv.GetSysdbaConnectString(),
25-
mode = cx_Oracle.SYSDBA)
24+
conn = cx_Oracle.connect(SampleEnv.GetAdminConnectString())
2625
DropSamples(conn)
2726
print("Done.")
28-

samples/SampleEnv.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
# CX_ORACLE_SAMPLES_EDITION_PASSWORD: password of user for editioning
1717
# CX_ORACLE_SAMPLES_EDITION_NAME: name of edition for editioning
1818
# CX_ORACLE_SAMPLES_CONNECT_STRING: connect string
19-
# CX_ORACLE_SAMPLES_SYSDBA_USER: SYSDBA user for setting up samples
20-
# CX_ORACLE_SAMPLES_SYSDBA_PASSWORD: SYSDBA password for setting up samples
19+
# CX_ORACLE_SAMPLES_DRCP_CONNECT_STRING: DRCP connect string
20+
# CX_ORACLE_SAMPLES_ADMIN_USER: admin user for setting up samples
21+
# CX_ORACLE_SAMPLES_ADMIN_PASSWORD: admin password for setting up samples
2122
#
2223
# CX_ORACLE_SAMPLES_CONNECT_STRING can be set to an Easy Connect string, or a
2324
# Net Service Name from a tnsnames.ora file or external naming service,
@@ -35,6 +36,9 @@
3536
# location such as $ORACLE_HOME/network/admin/tnsnames.ora or
3637
# /etc/tnsnames.ora. Alternatively set the TNS_ADMIN environment
3738
# variable and put the file in $TNS_ADMIN/tnsnames.ora.
39+
#
40+
# The administrative user for cloud databases is ADMIN and the administrative
41+
# user for on premises databases is SYSTEM.
3842
#------------------------------------------------------------------------------
3943

4044
from __future__ import print_function
@@ -54,6 +58,7 @@
5458
DEFAULT_EDITION_USER = "pythoneditions"
5559
DEFAULT_EDITION_NAME = "python_e1"
5660
DEFAULT_CONNECT_STRING = "localhost/orclpdb1"
61+
DEFAULT_DRCP_CONNECT_STRING = "localhost/orclpdb1:pooled"
5762

5863
# dictionary containing all parameters; these are acquired as needed by the
5964
# methods below (which should be used instead of consulting this dictionary
@@ -103,17 +108,18 @@ def GetMainConnectString(password=None):
103108
return "%s/%s@%s" % (GetMainUser(), password, GetConnectString())
104109

105110
def GetDrcpConnectString():
106-
return GetMainConnectString() + ":pooled"
111+
connectString = GetValue("DRCP_CONNECT_STRING", "DRCP Connect String",
112+
DEFAULT_DRCP_CONNECT_STRING)
113+
return "%s/%s@%s" % (GetMainUser(), GetMainPassword(), connectString)
107114

108115
def GetEditionConnectString():
109116
return "%s/%s@%s" % \
110117
(GetEditionUser(), GetEditionPassword(), GetConnectString())
111118

112-
def GetSysdbaConnectString():
113-
sysdbaUser = GetValue("SYSDBA_USER", "SYSDBA user", "sys")
114-
sysdbaPassword = GetValue("SYSDBA_PASSWORD",
115-
"Password for %s" % sysdbaUser)
116-
return "%s/%s@%s" % (sysdbaUser, sysdbaPassword, GetConnectString())
119+
def GetAdminConnectString():
120+
adminUser = GetValue("ADMIN_USER", "Administrative user", "admin")
121+
adminPassword = GetValue("ADMIN_PASSWORD", "Password for %s" % adminUser)
122+
return "%s/%s@%s" % (adminUser, adminPassword, GetConnectString())
117123

118124
def RunSqlScript(conn, scriptName, **kwargs):
119125
statementParts = []
@@ -128,7 +134,11 @@ def RunSqlScript(conn, scriptName, **kwargs):
128134
if statement:
129135
for searchValue, replaceValue in replaceValues:
130136
statement = statement.replace(searchValue, replaceValue)
131-
cursor.execute(statement)
137+
try:
138+
cursor.execute(statement)
139+
except:
140+
print("Failed to execute SQL:", statement)
141+
raise
132142
statementParts = []
133143
else:
134144
statementParts.append(line)
@@ -145,4 +155,3 @@ def RunSqlScript(conn, scriptName, **kwargs):
145155
prevName = name
146156
prevObjType = objType
147157
print(" %s/%s %s" % (lineNum, position, text))
148-

samples/SetupSamples.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
import SampleEnv
1818
import DropSamples
1919

20-
# connect as SYSDBA
21-
conn = cx_Oracle.connect(SampleEnv.GetSysdbaConnectString(),
22-
mode = cx_Oracle.SYSDBA)
20+
# connect as administrative user (usually SYSTEM or ADMIN)
21+
conn = cx_Oracle.connect(SampleEnv.GetAdminConnectString())
2322

2423
# drop existing users and editions, if applicable
2524
DropSamples.DropSamples(conn)

samples/Threads.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
SampleEnv.GetMainPassword(), SampleEnv.GetConnectString(), min=2,
2424
max=5, increment=1, threaded=True)
2525

26+
# dbms_session.sleep() replaces dbms_lock.sleep() from Oracle Database 18c
27+
conn = pool.acquire()
28+
sleepProcName = "dbms_session.sleep" \
29+
if int(conn.version.split(".")[0]) >= 18 \
30+
else "dbms_lock.sleep"
31+
conn.close()
32+
2633
def TheLongQuery():
2734
conn = pool.acquire()
2835
cursor = conn.cursor()
@@ -50,7 +57,7 @@ def DoALock():
5057
conn = pool.acquire()
5158
cursor = conn.cursor()
5259
print("DoALock(): beginning execute...")
53-
cursor.callproc("dbms_lock.sleep", (5,))
60+
cursor.callproc(sleepProcName, (5,))
5461
print("DoALock(): done execute...")
5562

5663

samples/sql/SetupSamplesExec.sql

Lines changed: 17 additions & 4 deletions

0 commit comments

Comments
 (0)