connection
DatabaseConnection module - provides async database connection management.
DatabaseConnection
Wrapper around SQLAlchemy AsyncEngine that provides a databases-compatible API.
Source code in ormar/databases/connection.py
| Python | |
|---|---|
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
atomic_execute_many
property
Whether execute_many outside a transaction is atomic on its own
(native asyncpg), so a single batch needs no explicit transaction.
bulk_create uses it to skip BEGIN/COMMIT around a lone
executemany: on asyncpg a failing row already leaves no rows
inserted. Other drivers give no such guarantee, so a batch there
still runs in a transaction.
Returns:
| Type | Description |
|---|---|
bool
|
True when a lone batch needs no transaction |
dialect
property
Get the database dialect.
engine
property
Get the async engine.
is_connected
property
Check if the engine is connected.
url
property
Get the database URL.
__aenter__()
async
Async context manager entry - connect to a database.
Source code in ormar/databases/connection.py
| Python | |
|---|---|
282 283 284 285 | |
__aexit__(exc_type, exc_val, exc_tb)
async
Async context manager exit - disconnect from database.
Source code in ormar/databases/connection.py
| Python | |
|---|---|
287 288 289 | |
__init__(url, native_execution=True, **options)
Initialize database connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Database URL with an async driver (e.g., postgresql+asyncpg://) |
required |
native_execution
|
bool
|
run statements directly on the driver for asyncpg, asyncmy and aiosqlite instead of through SQLAlchemy's execution layer |
True
|
options
|
Any
|
Additional engine options passed to
|
{}
|
Source code in ormar/databases/connection.py
| Python | |
|---|---|
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
connect()
async
Connect to the database by creating the async engine.
Source code in ormar/databases/connection.py
| Python | |
|---|---|
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
connection()
async
Get a connection from the pool. If inside a transaction, returns the transaction connection.
Source code in ormar/databases/connection.py
| Python | |
|---|---|
171 172 173 174 175 176 177 178 179 180 181 182 183 | |
disconnect()
async
Disconnect from the database and dispose of the engine. In case of force_rollback, also roll back the global transaction.
Source code in ormar/databases/connection.py
| Python | |
|---|---|
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
get_query_executor(*, transactional=False)
async
Get connection, reusing transaction connection if in transaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transactional
|
bool
|
If True, open an explicit transaction for the
scope of the executor. Use for server-side cursors ( |
False
|
Returns:
| Type | Description |
|---|---|
QueryExecutor
|
QueryExecutor wrapping a connection |
Source code in ormar/databases/connection.py
| Python | |
|---|---|
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
get_transaction_connection()
Get the current transaction connection if in a transaction.
Source code in ormar/databases/connection.py
| Python | |
|---|---|
274 275 276 | |
restore_transaction_isolation(conn)
async
Switch a freshly checked out connection to the server's default isolation level when the engine runs in AUTOCOMMIT, so explicit transactions get a real BEGIN / COMMIT. The pool resets it back to AUTOCOMMIT on checkin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conn
|
AsyncConnection
|
connection checked out from the main engine |
required |
Source code in ormar/databases/connection.py
| Python | |
|---|---|
185 186 187 188 189 190 191 192 193 194 195 196 197 | |
set_transaction_connection(conn)
Set the current transaction connection.
Source code in ormar/databases/connection.py
| Python | |
|---|---|
278 279 280 | |
start_driver_transaction(connection)
async
Make sure the driver-level transaction is open on a connection SQLAlchemy began a transaction on, so natively executed statements run inside it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection
|
AsyncConnection
|
connection with a begun transaction |
required |
Source code in ormar/databases/connection.py
| Python | |
|---|---|
260 261 262 263 264 265 266 267 268 269 270 271 272 | |
transaction(force_rollback=False)
Create a transaction context manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
force_rollback
|
bool
|
If True, always rollback (used for testing) |
False
|
Source code in ormar/databases/connection.py
| Python | |
|---|---|
199 200 201 202 203 204 205 | |
build_native_backend(dialect, enabled)
Build the native backend for the dialect's driver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dialect
|
Dialect
|
dialect of the connected database |
required |
enabled
|
bool
|
whether native execution is enabled |
required |
Returns:
| Type | Description |
|---|---|
Optional[NativeBackend]
|
native backend, or None when disabled or the driver has no adapter |
Source code in ormar/databases/connection.py
| Python | |
|---|---|
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |