|
1 | 1 | # -----------------------------------------------------------------------------
|
2 |
| -# Copyright (c) 2023, 2024, Oracle and/or its affiliates. |
| 2 | +# Copyright (c) 2023, 2025, Oracle and/or its affiliates. |
3 | 3 | #
|
4 | 4 | # This software is dual-licensed to you under the Universal Permissive License
|
5 | 5 | # (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
|
|
27 | 27 | #
|
28 | 28 | # Demonstrates using a connection pool with asyncio and gather().
|
29 | 29 | #
|
30 |
| -# Multiple database sessions will be opened and used by each coroutine. The |
31 |
| -# number of connections opened can depend on the speed of your environment. |
| 30 | +# This also shows the use of pool_alias for connection pool caching, so the |
| 31 | +# pool handle does not need to passed through the app. |
| 32 | +# |
| 33 | +# Each coroutine invocation will acquire a connection from the connection pool. |
| 34 | +# The number of connections in the pool will depend on the speed of your |
| 35 | +# environment. In some cases existing connections will get reused. In other |
| 36 | +# cases up to CONCURRENCY connections will be created by the pool. |
32 | 37 | # -----------------------------------------------------------------------------
|
33 | 38 |
|
34 | 39 | import asyncio
|
35 | 40 |
|
36 | 41 | import oracledb
|
37 | 42 | import sample_env
|
38 | 43 |
|
| 44 | +# Pool name for the connection pool cache |
| 45 | +POOL_ALIAS_NAME = "mypool" |
| 46 | + |
39 | 47 | # Number of coroutines to run
|
40 | 48 | CONCURRENCY = 5
|
41 | 49 |
|
42 | 50 | # Query the unique session identifier/serial number combination of a connection
|
43 |
| -SQL = """SELECT UNIQUE CURRENT_TIMESTAMP AS CT, sid||'-'||serial# AS SIDSER |
44 |
| - FROM v$session_connect_info |
45 |
| - WHERE sid = SYS_CONTEXT('USERENV', 'SID')""" |
| 51 | +SQL = """select unique current_timestamp as ct, sid||'-'||serial# as sidser |
| 52 | + from v$session_connect_info |
| 53 | + where sid = sys_context('userenv', 'sid')""" |
46 | 54 |
|
47 | 55 |
|
48 | 56 | # Show the unique session identifier/serial number of each connection that the
|
49 |
| -# pool opens |
| 57 | +# pool creates |
50 | 58 | async def init_session(connection, requested_tag):
|
51 | 59 | res = await connection.fetchone(SQL)
|
52 | 60 | print(res[0].strftime("%H:%M:%S.%f"), "- init_session SID-SERIAL#", res[1])
|
53 | 61 |
|
54 | 62 |
|
55 | 63 | # The coroutine simply shows the session identifier/serial number of the
|
56 |
| -# connection returned by the pool.acquire() call |
57 |
| -async def query(pool): |
58 |
| - async with pool.acquire() as connection: |
| 64 | +# connection returned from the pool |
| 65 | +async def query(): |
| 66 | + async with oracledb.connect_async( |
| 67 | + pool_alias=POOL_ALIAS_NAME |
| 68 | + ) as connection: |
59 | 69 | await connection.callproc("dbms_session.sleep", [1])
|
60 | 70 | res = await connection.fetchone(SQL)
|
61 | 71 | print(res[0].strftime("%H:%M:%S.%f"), "- query SID-SERIAL#", res[1])
|
62 | 72 |
|
63 | 73 |
|
64 | 74 | async def main():
|
65 |
| - pool = oracledb.create_pool_async( |
| 75 | + oracledb.create_pool_async( |
66 | 76 | user=sample_env.get_main_user(),
|
67 | 77 | password=sample_env.get_main_password(),
|
68 | 78 | dsn=sample_env.get_connect_string(),
|
69 | 79 | params=sample_env.get_pool_params(),
|
70 | 80 | min=1,
|
71 | 81 | max=CONCURRENCY,
|
72 | 82 | session_callback=init_session,
|
| 83 | + pool_alias=POOL_ALIAS_NAME, |
73 | 84 | )
|
74 | 85 |
|
75 |
| - coroutines = [query(pool) for i in range(CONCURRENCY)] |
| 86 | + coroutines = [query() for i in range(CONCURRENCY)] |
76 | 87 |
|
77 | 88 | await asyncio.gather(*coroutines)
|
78 | 89 |
|
| 90 | + pool = oracledb.get_pool(POOL_ALIAS_NAME) |
79 | 91 | await pool.close()
|
80 | 92 |
|
81 | 93 |
|
|
0 commit comments