|
| 1 | +.. _asyncconnobj: |
| 2 | + |
| 3 | +**************************** |
| 4 | +API: AsyncConnection Objects |
| 5 | +**************************** |
| 6 | + |
| 7 | +An AsyncConnection object can be created with :meth:`oracledb.connect_async()` |
| 8 | +or with :meth:`AsyncConnectionPool.acquire()`. AsyncConnections support use of |
| 9 | +concurrent programming with `asyncio <https://docs.python.org/3/library/ |
| 10 | +asyncio.html>`__. Unless explicitly noted as synchronous, the AsyncConnection |
| 11 | +methods should be used with ``await``. This object is an extension to the DB |
| 12 | +API. |
| 13 | + |
| 14 | +.. versionadded:: 2.0.0 |
| 15 | + |
| 16 | +.. note:: |
| 17 | + |
| 18 | + The Asynchronous I/O (asyncio) support in python-oracledb 2.0.0 is a |
| 19 | + pre-release and may change in the next version. |
| 20 | + |
| 21 | +.. note:: |
| 22 | + |
| 23 | + AsyncConnection objects are only supported in the python-oracledb Thin |
| 24 | + mode. |
| 25 | + |
| 26 | +.. note:: |
| 27 | + |
| 28 | + Any outstanding database transaction will be rolled back when the |
| 29 | + connection object is destroyed or closed. You must perform a |
| 30 | + :meth:`commit <AsyncConnection.commit>` first if you want data to |
| 31 | + persist in the database, see :ref:`txnasync`. |
| 32 | + |
| 33 | +.. _asyncconnmeth: |
| 34 | + |
| 35 | +AsyncConnection Methods |
| 36 | +======================= |
| 37 | + |
| 38 | +.. method:: AsyncConnection.__aenter__() |
| 39 | + |
| 40 | + The entry point for the asynchronous connection as a context manager. It |
| 41 | + returns itself. |
| 42 | + |
| 43 | +.. method:: AsyncConnection.__aexit__() |
| 44 | + |
| 45 | + The exit point for the asynchronous connection as a context manager. This |
| 46 | + will close the connection and roll back any uncommitted transaction. |
| 47 | + |
| 48 | +.. method:: AsyncConnection.callfunc(name, return_type, parameters=[], \ |
| 49 | + keyword_parameters={}) |
| 50 | + |
| 51 | + Calls a PL/SQL function with the given name. |
| 52 | + |
| 53 | + This is a shortcut for creating a cursor, calling the stored function with |
| 54 | + the cursor, and then closing the cursor. |
| 55 | + |
| 56 | +.. method:: AsyncConnection.callproc(name, parameters=[], \ |
| 57 | + keyword_parameters={}) |
| 58 | + |
| 59 | + Calls a PL/SQL procedure with the given name. |
| 60 | + |
| 61 | + This is a shortcut for creating a cursor, calling the stored procedure |
| 62 | + with the cursor, and then closing the cursor. |
| 63 | + |
| 64 | +.. method:: AsyncConnection.cancel() |
| 65 | + |
| 66 | + A synchronous method that breaks a long-running statement. |
| 67 | + |
| 68 | +.. method:: AsyncConnection.changepassword(old_password, new_password) |
| 69 | + |
| 70 | + Changes the password for the user to which the connection is connected. |
| 71 | + |
| 72 | +.. method:: AsyncConnection.close() |
| 73 | + |
| 74 | + Closes the connection. |
| 75 | + |
| 76 | +.. method:: AsyncConnection.commit() |
| 77 | + |
| 78 | + Commits any pending transaction to the database. |
| 79 | + |
| 80 | +.. method:: AsyncConnection.createlob(lob_type) |
| 81 | + |
| 82 | + Creates and returns a new temporary LOB of the specified type. |
| 83 | + |
| 84 | +.. method:: AsyncConnection.cursor(scrollable=False) |
| 85 | + |
| 86 | + A synchronous method that returns a cursor associated with the connection. |
| 87 | + |
| 88 | +.. method:: AsyncConnection.execute(statement, parameters=[]) |
| 89 | + |
| 90 | + Executes a statement against the database. |
| 91 | + |
| 92 | + This is a shortcut for creating a cursor, executing a statement with the |
| 93 | + cursor, and then closing the cursor. |
| 94 | + |
| 95 | +.. method:: AsyncConnection.executemany(statement, parameters=[]) |
| 96 | + |
| 97 | + Prepares a statement for execution against a database and then executes it |
| 98 | + against all parameter mappings or sequences found in the sequence |
| 99 | + parameters. |
| 100 | + |
| 101 | + This is a shortcut for creating a cursor, calling |
| 102 | + :meth:`AsyncCursor.executemany()` on the cursor, and then closing the |
| 103 | + cursor. |
| 104 | + |
| 105 | +.. method:: AsyncConnection.fetchall(statement, parameters=None, \ |
| 106 | + arraysize=None, rowfactory=None) |
| 107 | + |
| 108 | + Executes a query and returns all of the rows. After the rows are |
| 109 | + fetched, the cursor is closed. |
| 110 | + |
| 111 | +.. method:: AsyncConnection.fetchmany(statement, parameters=None, \ |
| 112 | + num_rows=None, rowfactory=None) |
| 113 | + |
| 114 | + Executes a query and returns up to the specified number of rows. After the |
| 115 | + rows are fetched, the cursor is closed. |
| 116 | + |
| 117 | +.. method:: AsyncConnection.fetchone(statement, parameters=None, \ |
| 118 | + rowfactory=None) |
| 119 | + |
| 120 | + Executes a query and returns the first row of the result set if one exists |
| 121 | + (or None if no rows exist). After the row is fetched, the cursor is |
| 122 | + closed. |
| 123 | + |
| 124 | +.. method:: AsyncConnection.gettype(name) |
| 125 | + |
| 126 | + Returns a :ref:`type object <dbobjecttype>` given its name. This can then |
| 127 | + be used to create objects which can be bound to cursors created by this |
| 128 | + connection. |
| 129 | + |
| 130 | +.. method:: AsyncConnection.is_healthy() |
| 131 | + |
| 132 | + A synchronous method that returns a boolean indicating the health status |
| 133 | + of a connection. |
| 134 | + |
| 135 | + Connections may become unusable in several cases, such as, if the network |
| 136 | + socket is broken, if an Oracle error indicates the connection is unusable, |
| 137 | + or, after receiving a planned down notification from the database. |
| 138 | + |
| 139 | + This function is best used before starting a new database request on an |
| 140 | + existing standalone connection. Pooled connections internally perform this |
| 141 | + check before returning a connection to the application. |
| 142 | + |
| 143 | + If this function returns False, the connection should be not be used by the |
| 144 | + application and a new connection should be established instead. |
| 145 | + |
| 146 | + This function performs a local check. To fully check a connection's health, |
| 147 | + use :meth:`AsyncConnection.ping()` which performs a round-trip to the |
| 148 | + database. |
| 149 | + |
| 150 | +.. method:: AsyncConnection.ping() |
| 151 | + |
| 152 | + Pings the database to verify if the connection is valid. |
| 153 | + |
| 154 | +.. method:: AsyncConnection.rollback() |
| 155 | + |
| 156 | + Rolls back any pending transaction. |
| 157 | + |
| 158 | +.. _asynconnattr: |
| 159 | + |
| 160 | +AsyncConnection Attributes |
| 161 | +========================== |
| 162 | + |
| 163 | +.. attribute:: AsyncConnection.action |
| 164 | + |
| 165 | + This write-only attribute sets the action column in the v$session table. It |
| 166 | + is a string attribute but the value None is accepted and treated as an |
| 167 | + empty string. |
| 168 | + |
| 169 | +.. attribute:: AsyncConnection.autocommit |
| 170 | + |
| 171 | + This read-write attribute determines whether autocommit mode is on or off. |
| 172 | + When autocommit mode is on, all statements are committed as soon as they |
| 173 | + have completed executing. |
| 174 | + |
| 175 | +.. attribute:: AsyncConnection.call_timeout |
| 176 | + |
| 177 | + This read-write attribute specifies the amount of time (in milliseconds) |
| 178 | + that a single round-trip to the database may take before a timeout will |
| 179 | + occur. A value of 0 means that no timeout will take place. |
| 180 | + |
| 181 | + If a timeout occurs, the error *DPI-1067* will be returned if the |
| 182 | + connection is still usable. Alternatively the error *DPI-1080* will be |
| 183 | + returned if the connection has become invalid and can no longer be used. |
| 184 | + |
| 185 | +.. attribute:: AsyncConnection.client_identifier |
| 186 | + |
| 187 | + This write-only attribute sets the client_identifier column in the |
| 188 | + v$session table. |
| 189 | + |
| 190 | +.. attribute:: AsyncConnection.clientinfo |
| 191 | + |
| 192 | + This write-only attribute sets the client_info column in the v$session |
| 193 | + table. |
| 194 | + |
| 195 | +.. attribute:: AsyncConnection.current_schema |
| 196 | + |
| 197 | + This read-write attribute sets the current schema attribute for the |
| 198 | + session. Setting this value is the same as executing the SQL statement |
| 199 | + ``ALTER SESSION SET CURRENT_SCHEMA``. The attribute is set (and verified) on |
| 200 | + the next call that does a round trip to the server. The value is placed |
| 201 | + before unqualified database objects in SQL statements you then execute. |
| 202 | + |
| 203 | +.. attribute:: AsyncConnection.db_domain |
| 204 | + |
| 205 | + This read-only attribute specifies the Oracle Database domain name |
| 206 | + associated with the connection. It is the same value returned by the SQL |
| 207 | + ``SELECT value FROM V$PARAMETER WHERE NAME = 'db_domain'``. |
| 208 | + |
| 209 | +.. attribute:: AsyncConnection.db_name |
| 210 | + |
| 211 | + This read-only attribute specifies the Oracle Database name associated with |
| 212 | + the connection. It is the same value returned by the SQL |
| 213 | + ``SELECT NAME FROM V$DATABASE``. |
| 214 | + |
| 215 | +.. attribute:: AsyncConnection.dbop |
| 216 | + |
| 217 | + This write-only attribute sets the database operation that is to be |
| 218 | + monitored. This can be viewed in the ``DBOP_NAME`` column of the |
| 219 | + ``v$sql_monitor`` table. |
| 220 | + |
| 221 | +.. attribute:: AsyncConnection.dsn |
| 222 | + |
| 223 | + This read-only attribute returns the TNS entry of the database to which a |
| 224 | + connection has been established. |
| 225 | + |
| 226 | +.. attribute:: AsyncConnection.econtext_id |
| 227 | + |
| 228 | + This write-only attribute specifies the execution context id. This |
| 229 | + value can be found as ecid in the v$session table and econtext_id in the |
| 230 | + auditing tables. The maximum length is 64 bytes. |
| 231 | + |
| 232 | +.. attribute:: AsyncConnection.edition |
| 233 | + |
| 234 | + This read-only attribute gets the session edition and is only available in |
| 235 | + Oracle Database 11.2 (the server must be at this level or higher for this |
| 236 | + to work). This attribute is ignored in python-oracledb Thin mode. |
| 237 | + |
| 238 | +.. attribute:: AsyncConnection.external_name |
| 239 | + |
| 240 | + This read-write attribute specifies the external name that is used by the |
| 241 | + connection when logging distributed transactions. |
| 242 | + |
| 243 | +.. attribute:: AsyncConnection.inputtypehandler |
| 244 | + |
| 245 | + This read-write attribute specifies a method called for each value that is |
| 246 | + bound to a statement executed on any cursor associated with this |
| 247 | + connection. The method signature is handler(cursor, value, arraysize) and |
| 248 | + the return value is expected to be a variable object or None in which case |
| 249 | + a default variable object will be created. If this attribute is None, the |
| 250 | + default behavior will take place for all values bound to statements. |
| 251 | + |
| 252 | +.. attribute:: AsyncConnection.instance_name |
| 253 | + |
| 254 | + This read-only attribute specifies the Oracle Database instance name |
| 255 | + associated with the connection. It is the same value as the SQL expression |
| 256 | + ``sys_context('userenv', 'instance_name')``. |
| 257 | + |
| 258 | +.. attribute:: AsyncConnection.internal_name |
| 259 | + |
| 260 | + This read-write attribute specifies the internal name that is used by the |
| 261 | + connection when logging distributed transactions. |
| 262 | + |
| 263 | +.. attribute:: AsyncConnection.ltxid |
| 264 | + |
| 265 | + This read-only attribute returns the logical transaction id for the |
| 266 | + connection. It is used within Oracle Transaction Guard as a means of |
| 267 | + ensuring that transactions are not duplicated. See the Oracle documentation |
| 268 | + and the provided sample for more information. |
| 269 | + |
| 270 | + .. note: |
| 271 | +
|
| 272 | + This attribute is only available when Oracle Database 12.1 or later is |
| 273 | + in use |
| 274 | +
|
| 275 | +.. attribute:: AsyncConnection.max_open_cursors |
| 276 | + |
| 277 | + This read-only attribute specifies the maximum number of cursors that the |
| 278 | + database can have open concurrently. It is the same value returned by the |
| 279 | + SQL ``SELECT VALUE FROM V$PARAMETER WHERE NAME = 'open_cursors'``. |
| 280 | + |
| 281 | +.. attribute:: AsyncConnection.module |
| 282 | + |
| 283 | + This write-only attribute sets the module column in the v$session table. |
| 284 | + The maximum length for this string is 48 and if you exceed this length you |
| 285 | + will get ORA-24960. |
| 286 | + |
| 287 | +.. attribute:: AsyncConnection.outputtypehandler |
| 288 | + |
| 289 | + This read-write attribute specifies a method called for each column that is |
| 290 | + going to be fetched from any cursor associated with this connection. The |
| 291 | + method signature is ``handler(cursor, metadata)`` and the return value is |
| 292 | + expected to be a :ref:`variable object<varobj>` or None in which case a |
| 293 | + default variable object will be created. If this attribute is None, the |
| 294 | + default behavior will take place for all columns fetched from cursors. |
| 295 | + |
| 296 | + See :ref:`outputtypehandlers`. |
| 297 | + |
| 298 | +.. attribute:: AsyncConnection.sdu |
| 299 | + |
| 300 | + This read-only attribute specifies the size of the Session Data Unit (SDU) |
| 301 | + that is being used by the connection. The value will be the lesser of the |
| 302 | + requested python-oracledb size and the maximum size allowed by the database |
| 303 | + network configuration. |
| 304 | + |
| 305 | +.. attribute:: AsyncConnection.service_name |
| 306 | + |
| 307 | + This read-only attribute specifies the Oracle Database service name |
| 308 | + associated with the connection. This is the same value returned by the SQL |
| 309 | + ``SELECT SYS_CONTEXT('USERENV', 'SERVICE_NAME') FROM DUAL``. |
| 310 | + |
| 311 | +.. attribute:: AsyncConnection.stmtcachesize |
| 312 | + |
| 313 | + This read-write attribute specifies the size of the statement cache. This |
| 314 | + value can make a significant difference in performance if you have a small |
| 315 | + number of statements that you execute repeatedly. |
| 316 | + |
| 317 | + The default value is 20. |
| 318 | + |
| 319 | + See :ref:`Statement Caching <stmtcache>` for more information. |
| 320 | + |
| 321 | +.. attribute:: AsyncConnection.transaction_in_progress |
| 322 | + |
| 323 | + This read-only attribute specifies whether a transaction is currently in |
| 324 | + progress on the database associated with the connection. |
| 325 | + |
| 326 | +.. attribute:: AsyncConnection.username |
| 327 | + |
| 328 | + This read-only attribute returns the name of the user which established the |
| 329 | + connection to the database. |
| 330 | + |
| 331 | +.. attribute:: AsyncConnection.version |
| 332 | + |
| 333 | + This read-only attribute returns the version of the database to which a |
| 334 | + connection has been established. |
0 commit comments