Skip to content

Commit 5df9a73

Browse files
Add cx_Oracle.init_oracle_client() for Oracle Client library initialization;
change default encoding to UTF-8.
1 parent c144216 commit 5df9a73

13 files changed

+811
-369
lines changed

doc/src/api_manual/module.rst

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ Module Interface
9595
the password for the logon during the connection process.
9696

9797
See the :ref:`globalization <globalization>` section for details on the
98-
encoding and nencoding parameters.
98+
encoding and nencoding parameters. Note the default encoding and nencoding
99+
values changed to "UTF-8" in cx_Oracle 8, and any character set in NLS_LANG
100+
is ignored.
99101

100102
The edition parameter is expected to be a string if specified and sets the
101103
edition to use for the session. It is only relevant if both the client and
@@ -146,6 +148,47 @@ Module Interface
146148
time module for details).
147149

148150

151+
.. function:: init_oracle_client(lib_dir=None, config_dir=None, \
152+
error_url=None, driver_name=None)
153+
154+
Initialize the Oracle client library now, rather than when
155+
:func:`cx_Oracle.clientversion()`, :func:`cx_Oracle.connect()` or
156+
:func:`cx_Oracle.SessionPool()` is called for the first time. If
157+
initialization has already taken place, an exception is raised.
158+
159+
If the parameter `lib_dir` is not `None` or the empty string,
160+
the specified directory is the only one searched for the Oracle Client
161+
libraries; otherwise, the standard way of locating the Oracle Client
162+
library is used.
163+
164+
If the parameter `config_dir` is not `None` or the empty string, the
165+
specified directory is used to find Oracle Client library configuration
166+
files. This is equivalent to setting the environment variable `TNS_ADMIN`
167+
and overrides any value already set in `TNS_ADMIN`. If this parameter is not
168+
set, the standard way of locating Oracle Client library configuration files
169+
is used.
170+
171+
If the parameter `error_url` is not `None` or the empty string, the
172+
specified value is included in the message of the exception raised when the
173+
Oracle Client library cannot be loaded; otherwise, the :ref:`installation`
174+
URL is included.
175+
176+
If the parameter `driver_name` is not `None` or the empty string, the
177+
specified value can be found in database views that give information about
178+
connections. For example, it is in the ``CLIENT_DRIVER`` column of
179+
``V$SESSION_CONNECT_INFO``. The standard is to set this value to
180+
``"<name> : version>"``, where <name> is the name of the driver and
181+
<version> is its version. Theere should be a single space character before
182+
and after the colon. If this value is not specified, then the default
183+
value of "cx_Oracle : <version>" is used.
184+
185+
See :ref:`initialization` for more discussion.
186+
187+
.. note::
188+
189+
This method is an extension to the DB API definition.
190+
191+
149192
.. function:: makedsn(host, port, sid=None, service_name=None, region=None, \
150193
sharding_key=None, super_sharding_key=None)
151194

@@ -207,7 +250,9 @@ Module Interface
207250
system authentication and Oracle wallets.
208251

209252
See the :ref:`globalization <globalization>` section for details on the
210-
encoding and nencoding parameters.
253+
encoding and nencoding parameters. Note the default encoding and nencoding
254+
values changed to "UTF-8" in cx_Oracle 8, and any character set in NLS_LANG
255+
is ignored.
211256

212257
The edition parameter is expected to be a string, if specified, and sets
213258
the edition to use for the sessions in the pool. It is only relevant if

doc/src/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
# General substitutions.
3535
project = 'cx_Oracle'
36-
copyright = u'2016, 2019, Oracle and/or its affiliates. All rights reserved. Portions Copyright © 2007-2015, Anthony Tuininga. All rights reserved. Portions Copyright © 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta, Canada. All rights reserved'
36+
copyright = u'2016, 2020, Oracle and/or its affiliates. All rights reserved. Portions Copyright © 2007-2015, Anthony Tuininga. All rights reserved. Portions Copyright © 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta, Canada. All rights reserved'
3737
author = 'Oracle'
3838

3939
# The default replacements for |version| and |release|, also used in various

doc/src/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ User Guide
2121

2222
user_guide/introduction.rst
2323
user_guide/installation.rst
24+
user_guide/initialization.rst
2425
user_guide/connection_handling.rst
2526
user_guide/sql_execution.rst
2627
user_guide/plsql_execution.rst

doc/src/release_notes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Version 8.0 (TBD)
4444
- The string representation of variables has changed to include the type
4545
in addition to the value.
4646

47+
#) Added function :meth:`cx_Oracle.init_oracle_client()` in order to enable
48+
programmatic control of the initialization of the Oracle Client library.
4749
#) Added functions :meth:`SodaCollection.save()`,
4850
:meth:`SodaCollection.saveAndGet()` and :meth:`SodaCollection.truncate()`
4951
available in Oracle Client 20 and higher.

doc/src/user_guide/connection_handling.rst

Lines changed: 11 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
Connecting to Oracle Database
55
*****************************
66

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>`.
910

1011
Establishing Database Connections
1112
=================================
@@ -30,12 +31,10 @@ There are two ways to connect to Oracle Database using cx_Oracle:
3031
:meth:`SessionPool.acquire()` can be called to obtain a connection
3132
from a pool.
3233

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>`.
3938

4039
**Example: Standalone Connection to Oracle Database**
4140

@@ -74,131 +73,6 @@ This code ensures that, once the block is completed, the connection is closed
7473
and resources have been reclaimed by the database. In addition, any attempt to
7574
use the variable ``connection`` outside of the block will simply fail.
7675

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-
20276
.. _connstr:
20377

20478
Connection Strings
@@ -1253,7 +1127,7 @@ Securely Encrypting Network Traffic to Oracle Database
12531127
======================================================
12541128

12551129
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
12571131
view plain text values as the data passes over the network. The easiest
12581132
configuration is Oracle’s native network encryption. The standard SSL protocol
12591133
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:
13651239

13661240
The other files and the wallet password are not needed.
13671241

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>`.
13691243

13701244
Run Your Application
13711245
--------------------
@@ -1428,7 +1302,7 @@ database table can be split so each shard contains a table with the same columns
14281302
but a different subset of rows. These tables are known as sharded tables.
14291303
Sharding is configured in Oracle Database, see the `Oracle Sharding
14301304
<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.
14321306

14331307
The :meth:`cx_Oracle.connect()` and :meth:`SessionPool.acquire()` functions
14341308
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.
14391313

14401314
When creating a connection pool, the :meth:`cx_Oracle.SessionPool()` attribute
14411315
``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.
14431317

14441318
Shard key values may be of type string (mapping to VARCHAR2 shard keys), number
14451319
(NUMBER), bytes (RAW), or date (DATE). Multiple types may be used in each

0 commit comments

Comments
 (0)