4
4
Connecting to Oracle Database
5
5
*****************************
6
6
7
- This chapter covers connecting to Oracle Database using cx_Oracle. It
8
- explains the various forms of connections and how to manage them.
7
+ Connections between cx_Oracle and Oracle Database are used for executing
8
+ :ref: `SQL <sqlexecution >`, :ref: `PL/SQL <plsqlexecution >`, and :ref: `SODA
9
+ <sodausermanual>`.
9
10
10
11
Establishing Database Connections
11
12
=================================
@@ -30,12 +31,10 @@ There are two ways to connect to Oracle Database using cx_Oracle:
30
31
:meth: `SessionPool.acquire() ` can be called to obtain a connection
31
32
from a pool.
32
33
33
- Optional connection creation parameters allow you to utilize features such as
34
- :ref: `Sharding <connsharding >` and `Database Resident Connection Pooling
35
- (DRCP) `_.
36
-
37
- Once a connection is established, you can use it for SQL, PL/SQL and
38
- SODA.
34
+ Many connection behaviors can be controlled by cx_Oracle options. Other
35
+ settings can be configured in :ref: `optnetfiles ` or in :ref: `optclientfiles `.
36
+ These include limiting the amount of time that opening a connection can take, or
37
+ enabling :ref: `network encryption <netencrypt >`.
39
38
40
39
**Example: Standalone Connection to Oracle Database **
41
40
@@ -74,131 +73,6 @@ This code ensures that, once the block is completed, the connection is closed
74
73
and resources have been reclaimed by the database. In addition, any attempt to
75
74
use the variable ``connection `` outside of the block will simply fail.
76
75
77
-
78
- .. _envset :
79
-
80
- Oracle Environment Variables
81
- ============================
82
-
83
- Before running Python, ensure that any necessary Oracle environment
84
- variables are configured correctly. The variables needed by cx_Oracle
85
- depend on how Python is installed, how you connect to the database,
86
- and what optional settings are desired.
87
-
88
- .. list-table :: Common Oracle environment variables
89
- :header-rows: 1
90
- :widths: 1 2
91
- :align: left
92
-
93
- * - Oracle Environment Variables
94
- - Purpose
95
- * - ORACLE_HOME
96
- - The directory containing the Oracle Database software. The directory
97
- and various configuration files must be readable by the Python process.
98
- This variable should not be set if you are using Oracle Instant Client.
99
- * - LD_LIBRARY_PATH
100
- - The library search path for platforms like Linux should include the
101
- Oracle libraries, for example ``$ORACLE_HOME/lib `` or
102
- ``/opt/instantclient_19_3 ``. This variable is not needed if the
103
- libraries are located by an alternative method, such as with
104
- ``ldconfig ``. On other UNIX platforms you may need to set an OS
105
- specific equivalent, such as ``LIBPATH `` or ``SHLIB_PATH ``.
106
- * - PATH
107
- - The library search path for Windows should include the location where
108
- ``OCI.DLL `` is found.
109
- * - TNS_ADMIN
110
- - The directory of Oracle Database client configuration files such as
111
- ``tnsnames.ora `` and ``sqlnet.ora ``. Needed if the configuration files
112
- are in a non-default location. See :ref: `optnetfiles `.
113
- * - NLS_LANG
114
- - Determines the 'national language support' globalization options for
115
- cx_Oracle. If not set, a default value will be chosen by Oracle. See
116
- :ref: `globalization `.
117
- * - NLS_DATE_FORMAT, NLS_TIMESTAMP_FORMAT
118
- - Often set in Python applications to force a consistent date format
119
- independent of the locale. The variables are ignored if the environment
120
- variable ``NLS_LANG `` is not set.
121
-
122
- It is recommended to set Oracle variables in the environment before
123
- invoking Python. However, they may also be set in application code with
124
- ``os.putenv() `` before the first connection is established. Note that setting
125
- operating system variables such as ``LD_LIBRARY_PATH `` must be done
126
- before running Python.
127
-
128
-
129
- Optional Oracle Configuration Files
130
- ===================================
131
-
132
- .. _optnetfiles :
133
-
134
- Optional Oracle Net Configuration Files
135
- ---------------------------------------
136
-
137
- Optional Oracle Net configuration files affect connections and
138
- applications.
139
-
140
- Common files include:
141
-
142
- * ``tnsnames.ora ``: A configuration file that defines databases addresses
143
- for establishing connections. See :ref: `Net Service Name for Connection
144
- Strings <netservice>`.
145
-
146
- * ``sqlnet.ora ``: A profile configuration file that may contain information
147
- on features such as connection failover, network encryption, logging, and
148
- tracing. See `Oracle Net Services Reference
149
- <https://www.oracle.com/pls/topic/lookup?ctx=dblatest&
150
- id=GUID-19423B71-3F6C-430F-84CC-18145CC2A818> `__ for more information.
151
-
152
- * ``cwallet.sso ``: an Oracle wallet for secure connection.
153
-
154
- The default location for these files is the ``network/admin ``
155
- directory under the Oracle Instant Client installation directory or the
156
- ``$ORACLE_HOME `` directory (for full database or client installations). To use
157
- a non-default location, put the files in a directory that is accessible to
158
- Python and set the ``TNS_ADMIN `` environment variable to
159
- that directory path. For example, if the file
160
- ``/etc/my-oracle-config/tnsnames.ora `` is being used, set the
161
- ``TNS_ADMIN `` environment variable to ``/etc/my-oracle-config ``.
162
-
163
- Also see :ref: `Network Configuration <hanetwork >`.
164
-
165
- .. _optclientfiles :
166
-
167
- Optional Oracle Client Configuration Files
168
- ------------------------------------------
169
-
170
- When cx_Oracle uses Oracle Database Clients 12.1, or later, an optional client
171
- parameter file called ``oraaccess.xml `` can be used. This file can be used to
172
- override some application settings, which can be useful if the application
173
- cannot be altered. The file also enables auto-tuning of the client statement
174
- cache.
175
-
176
- The file is read from the same directory as the
177
- `Optional Oracle Net Configuration Files `_.
178
-
179
- A sample ``oraaccess.xml `` file that sets the Oracle client ‘prefetch’
180
- value to 50 rows and the 'client statement cache' value to 1, is shown
181
- below::
182
-
183
- <oraaccess xmlns="http://xmlns.oracle.com/oci/oraaccess"
184
- xmlns:oci="http://xmlns.oracle.com/oci/oraaccess"
185
- schemaLocation="http://xmlns.oracle.com/oci/oraaccess
186
- http://xmlns.oracle.com/oci/oraaccess.xsd">
187
- <default_parameters>
188
- <prefetch>
189
- <rows>50</rows>
190
- </prefetch>
191
- <statement_cache>
192
- <size>1</size>
193
- </statement_cache>
194
- </default_parameters>
195
- </oraaccess>
196
-
197
- Refer to the documentation on `oraaccess.xml
198
- <https://www.oracle.com/pls/topic/lookup?
199
- ctx=dblatest&id=GUID-9D12F489-EC02-46BE-8CD4-5AECED0E2BA2> `__
200
- for more details.
201
-
202
76
.. _connstr :
203
77
204
78
Connection Strings
@@ -1253,7 +1127,7 @@ Securely Encrypting Network Traffic to Oracle Database
1253
1127
======================================================
1254
1128
1255
1129
You can encrypt data transferred between the Oracle Database and the Oracle
1256
- client libraries used by cx_Oracle so that unauthorized parties are not able to
1130
+ Client libraries used by cx_Oracle so that unauthorized parties are not able to
1257
1131
view plain text values as the data passes over the network. The easiest
1258
1132
configuration is Oracle’s native network encryption. The standard SSL protocol
1259
1133
can also be used if you have a PKI, but setup is necessarily more involved.
@@ -1365,7 +1239,7 @@ For cx_Oracle, only these files from the zip are needed:
1365
1239
1366
1240
The other files and the wallet password are not needed.
1367
1241
1368
- Place these files as shown in `Optional Oracle Net Configuration Files `_ .
1242
+ Place these files as shown in :ref: `Optional Oracle Net Configuration Files < optnetfiles >` .
1369
1243
1370
1244
Run Your Application
1371
1245
--------------------
@@ -1428,7 +1302,7 @@ database table can be split so each shard contains a table with the same columns
1428
1302
but a different subset of rows. These tables are known as sharded tables.
1429
1303
Sharding is configured in Oracle Database, see the `Oracle Sharding
1430
1304
<https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=SHARD> `__ manual.
1431
- Sharding requires Oracle Database and client libraries 12.2, or later.
1305
+ Sharding requires Oracle Database and Oracle Client libraries 12.2, or later.
1432
1306
1433
1307
The :meth: `cx_Oracle.connect() ` and :meth: `SessionPool.acquire() ` functions
1434
1308
accept ``shardingkey `` and ``supershardingkey `` parameters that are a sequence
@@ -1439,7 +1313,7 @@ range (the super sharding key), and then further partitioned by a sharding key.
1439
1313
1440
1314
When creating a connection pool, the :meth: `cx_Oracle.SessionPool() ` attribute
1441
1315
``maxSessionsPerShard `` can be set. This is used to balance connections in the
1442
- pool equally across shards. It requires Oracle client libraries 18.3, or later.
1316
+ pool equally across shards. It requires Oracle Client libraries 18.3, or later.
1443
1317
1444
1318
Shard key values may be of type string (mapping to VARCHAR2 shard keys), number
1445
1319
(NUMBER), bytes (RAW), or date (DATE). Multiple types may be used in each
0 commit comments