@@ -22,14 +22,14 @@ There are two ways to connect to Oracle Database using cx_Oracle:
22
22
23
23
* **Pooled connections **
24
24
25
- Connection pooling is important for performance when applications
26
- frequently connect and disconnect from the database. Oracle high
27
- availability features in the pool implementation mean that small
28
- pools can also be useful for applications that want a few
29
- connections available for infrequent use. Pools are created with
30
- :meth: `cx_Oracle.SessionPool() ` and then
31
- :meth: `SessionPool.acquire() ` can be called to obtain a connection
32
- from a pool.
25
+ :ref: ` Connection pooling < connpool >` is important for performance when
26
+ applications frequently connect and disconnect from the database. Pools
27
+ support Oracle's :ref: ` high availability < highavailability >` features and are
28
+ recommended for applications that must be reliable. Small pools can also be
29
+ useful for applications that want a few connections available for infrequent
30
+ use. Pools are created with :meth: `cx_Oracle.SessionPool() ` at application
31
+ initialization time, and then :meth: `SessionPool.acquire() ` can be called to
32
+ obtain a connection from a pool.
33
33
34
34
Many connection behaviors can be controlled by cx_Oracle options. Other
35
35
settings can be configured in :ref: `optnetfiles ` or in :ref: `optclientfiles `.
@@ -57,8 +57,7 @@ Connections should be released when they are no longer needed by calling
57
57
:meth: `Connection.close() `. Alternatively, you may prefer to let connections
58
58
be automatically cleaned up when references to them go out of scope. This lets
59
59
cx_Oracle close dependent resources in the correct order. One other approach is
60
- the use of a "with" block, which ensures that a connection is closed once the
61
- block is completed. For example:
60
+ the use of a "with" block, for example:
62
61
63
62
.. code-block :: python
64
63
@@ -73,6 +72,9 @@ This code ensures that, once the block is completed, the connection is closed
73
72
and resources have been reclaimed by the database. In addition, any attempt to
74
73
use the variable ``connection `` outside of the block will simply fail.
75
74
75
+ Prompt closing of connections is important when using connection pools so
76
+ connections are available for reuse by other pool users.
77
+
76
78
.. _connstr :
77
79
78
80
Connection Strings
@@ -248,25 +250,38 @@ Connection Pooling
248
250
==================
249
251
250
252
cx_Oracle's connection pooling lets applications create and maintain a pool of
251
- connections to the database. The internal implementation uses Oracle's
252
- `session pool technology <https://www.oracle.com/pls/topic/lookup?ctx=dblatest&
253
- id=GUID-F9662FFB-EAEF-495C-96FC-49C6D1D9625C> `__.
254
- In general, each connection in a cx_Oracle connection pool corresponds to one
255
- Oracle session.
253
+ connections to the database. Connection pooling is important for performance
254
+ when applications frequently connect and disconnect from the database. The pool
255
+ implementation uses Oracle's `session pool technology
256
+ <https://www.oracle.com/pls/topic/lookup?ctx=dblatest&
257
+ id=GUID-F9662FFB-EAEF-495C-96FC-49C6D1D9625C> `__ which supports Oracle's
258
+ :ref: `high availability <highavailability >` features and is recommended for
259
+ applications that must be reliable. This also means that small pools can be
260
+ useful for applications that want a few connections available for infrequent
261
+ use.
256
262
257
263
A connection pool is created by calling :meth: `~cx_Oracle.SessionPool() `. This
258
- is generally called during application initialization. Connections can then be
259
- obtained from a pool by calling :meth: `~SessionPool.acquire() `. The initial
260
- pool size and the maximum pool size are provided at the time of pool creation.
261
- When the pool needs to grow, new connections are created automatically. The
262
- pool can shrink back to the minimum size when connections are no longer in use.
264
+ is generally called during application initialization. The initial pool size
265
+ and the maximum pool size are provided at the time of pool creation. When the
266
+ pool needs to grow, new connections are created automatically. The pool can
267
+ shrink back to the minimum size when connections are no longer in use. For
268
+ pools created with :ref: `external authentication <extauth >`, with
269
+ :ref: `homogeneous <connpooltypes >` set to False, or when using :ref: `drcp `, then
270
+ the number of connections initially created is zero even if a larger value is
271
+ specified for ``min ``. Also in these cases the pool increment is always 1,
272
+ regardless of the value of ``increment ``.
273
+
274
+ After a pool has been created, connections can be obtained from it by calling
275
+ :meth: `~SessionPool.acquire() `. These connections can be used in the same way
276
+ that standalone connections are used.
263
277
264
278
Connections acquired from the pool should be released back to the pool using
265
279
:meth: `SessionPool.release() ` or :meth: `Connection.close() ` when they are no
266
280
longer required. Otherwise, they will be released back to the pool
267
281
automatically when all of the variables referencing the connection go out of
268
- scope. The session pool can be completely closed using
269
- :meth: `SessionPool.close() `.
282
+ scope. This make connections available for other users of the pool.
283
+
284
+ The session pool can be completely closed using :meth: `SessionPool.close() `.
270
285
271
286
The example below shows how to connect to Oracle Database using a
272
287
connection pool:
@@ -291,6 +306,17 @@ connection pool:
291
306
# Close the pool
292
307
pool.close()
293
308
309
+ Other :meth: `cx_Oracle.SessionPool() ` options can be used at pool creation. For
310
+ example the ``getmode `` value can be set so that any ``aquire() `` call will wait
311
+ for a connection to become available if all are currently in use, for example:
312
+
313
+ .. code-block :: python
314
+
315
+ # Create the session pool
316
+ pool = cx_Oracle.SessionPool(" hr" , userpwd, " dbhost.example.com/orclpdb1" ,
317
+ min = 2 , max = 5 , increment = 1 , getmode = cx_Oracle.SPOOL_ATTRVAL_WAIT , encoding = " UTF-8" )
318
+
319
+
294
320
Applications that are using connections concurrently in multiple threads should
295
321
set the ``threaded `` parameter to *True * when creating a connection pool:
296
322
@@ -300,6 +326,7 @@ set the ``threaded`` parameter to *True* when creating a connection pool:
300
326
pool = cx_Oracle.SessionPool(" hr" , userpwd, " dbhost.example.com/orclpdb1" ,
301
327
min = 2 , max = 5 , increment = 1 , threaded = True , encoding = " UTF-8" )
302
328
329
+
303
330
See `ConnectionPool.py
304
331
<https://github.com/oracle/python-cx_Oracle/tree/master/samples/ConnectionPool.py> `__
305
332
for an example.
@@ -314,33 +341,49 @@ will also do a full :ref:`round-trip <roundtrips>` ping to the database when it
314
341
is about to return a connection that was unused in the pool for 60 seconds. If
315
342
the ping fails, the connection will be discarded and another one obtained before
316
343
:meth: `~SessionPool.acquire() ` returns to the application. Because this full
317
- ping is time based, it won't catch every failure. Also since network timeouts
318
- and session kills may occur after :meth: `~SessionPool.acquire() ` and before
319
- :meth: `Cursor.execute() `, applications need to check for errors after each
320
- :meth: `~Cursor.execute() ` and make application-specific decisions about retrying
321
- work if there was a connection failure. Note both the lightweight and full ping
322
- connection checks can mask configuration issues, for example firewalls killing
323
- connections, so monitor the connection rate in AWR for an unexpected value. You
324
- can explicitly initiate a full ping to check connection liveness with
325
- :meth: `Connection.ping() ` but overuse will impact performance and scalability.
344
+ ping is time based, it won't catch every failure. Also network timeouts and
345
+ session kills may occur after :meth: `~SessionPool.acquire() ` and before
346
+ :meth: `Cursor.execute() `. To handle these cases, applications need to check
347
+ for errors after each :meth: `~Cursor.execute() ` and make application-specific
348
+ decisions about retrying work if there was a connection failure. Oracle's
349
+ :ref: `Application Continuity <highavailability >` can do this automatically in
350
+ some cases. Note both the lightweight and full ping connection checks can mask
351
+ performance-impacting configuration issues, for example firewalls killing
352
+ connections, so monitor the connection rate in `AWR
353
+ <https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-56AEF38E-9400-427B-A818-EDEC145F7ACD> `__
354
+ for an unexpected value. You can explicitly initiate a full ping to check
355
+ connection liveness with :meth: `Connection.ping() ` but overuse will impact
356
+ performance and scalability.
357
+
358
+ Connection Pool Sizing
359
+ ----------------------
326
360
327
361
The Oracle Real-World Performance Group's recommendation is to use fixed size
328
- connection pools. The values of min and max should be the same (and the
329
- increment equal to zero). The :ref: `firewall <hanetwork >`, `resource manager
330
- <https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-2BEF5482-CF97-4A85-BD90-9195E41E74EF> `__
331
- or user profile `IDLE_TIME
332
- <https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-ABC7AE4D-64A8-4EA9-857D-BEF7300B64C3> `__
333
- should not expire idle sessions. This avoids connection storms which can
334
- decrease throughput. See `Guideline for Preventing Connection Storms: Use
335
- Static Pools
362
+ connection pools. The values of ``min `` and ``max `` should be the same (and the
363
+ ``increment `` equal to zero). This avoids connection storms which can decrease
364
+ throughput. See `Guideline for Preventing Connection Storms: Use Static Pools
336
365
<https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-7DFBA826-7CC0-4D16-B19C-31D168069B54> `__,
337
- which contains details about sizing of pools.
366
+ which contains more details about sizing of pools. Having a fixed size will
367
+ guarantee that the database can handle the upper pool size. For example, if a
368
+ pool needs to grow but the database resources are limited, then
369
+ :meth: `SessionPool.acquire() ` may return errors such as ORA-28547. With a fixed
370
+ pool size, this class of error will occur when the pool is created, allowing you
371
+ to change the size before users access the application. With a dynamically
372
+ growing pool, the error may occur much later after the pool has been in use for
373
+ some time.
338
374
339
375
The Real-World Performance Group also recommends keeping pool sizes small, as
340
376
they may perform better than larger pools. The pool attributes should be
341
377
adjusted to handle the desired workload within the bounds of available resources
342
378
in cx_Oracle and the database.
343
379
380
+ Make sure the :ref: `firewall <hanetwork >`, `resource manager
381
+ <https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-2BEF5482-CF97-4A85-BD90-9195E41E74EF> `__
382
+ or user profile `IDLE_TIME
383
+ <https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-ABC7AE4D-64A8-4EA9-857D-BEF7300B64C3> `__
384
+ do not expire idle sessions, since this will require connections be recreated,
385
+ which will impact performance and scalability.
386
+
344
387
.. _sessioncallback :
345
388
346
389
Session CallBacks for Setting Pooled Connection State
0 commit comments