Skip to content

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
class DatabaseConnection:
    """
    Wrapper around SQLAlchemy AsyncEngine that provides a databases-compatible API.
    """

    def __init__(self, url: str, native_execution: bool = True, **options: Any) -> None:
        """
        Initialize database connection.

        :param url: Database URL with an async driver (e.g., postgresql+asyncpg://)
        :param native_execution: run statements directly on the driver for
            asyncpg, asyncmy and aiosqlite instead of through SQLAlchemy's
            execution layer
        :param options: Additional engine options passed to
            ``create_async_engine``; ``pool_size`` defaults to 10 and
            ``max_overflow`` to 10. ``min_connections`` (default
            ``pool_size``) opens that many pooled connections on
            ``connect()`` (at most ``pool_size``), so the first concurrent
            queries do not wait for new connections; 0 keeps the pool lazy.
            ``json_serializer`` and ``json_deserializer`` of JSON columns
            default to orjson.
        """
        self._force_rollback = options.pop("force_rollback", False)
        self._url = url
        # Set reasonable pool defaults if not provided; overflow connections are
        # closed on checkin, so pool_size should cover the usual concurrency
        if "pool_size" not in options:
            options["pool_size"] = 10
        if "max_overflow" not in options:
            options["max_overflow"] = 10
        options.setdefault("json_serializer", serialize_json_column)
        options.setdefault("json_deserializer", deserialize_json_column)
        self._min_connections = options.pop("min_connections", options["pool_size"])
        self._options = options
        self._engine: Optional[AsyncEngine] = None
        self._autocommit_engine: Optional[AsyncEngine] = None
        self.rules: DialectRules = rules_for(
            backend_name=make_url(url).get_backend_name()
        )
        self._autocommit_by_default = (
            "isolation_level" not in options and self.rules.autocommit_engine
        )

        self._global_transaction: Optional[Transaction] = None
        self._native_execution = native_execution
        self.native_backend: Optional[NativeBackend] = None

    async def connect(self) -> None:
        """Connect to the database by creating the async engine."""
        if self._engine is None:
            engine_options = (
                {**self._options, "isolation_level": "AUTOCOMMIT"}
                if self._autocommit_by_default
                else self._options
            )
            self._engine = create_async_engine(self._url, **engine_options)
            # Engine (or a view of the same engine/pool) in AUTOCOMMIT mode.
            # Standalone queries use this to avoid a BEGIN/COMMIT round-trip
            # per call, matching legacy `databases`-library semantics. Explicit
            # Transactions keep using ``self._engine`` so BEGIN / SAVEPOINT
            # still work.
            self._autocommit_engine = (
                self._engine
                if self._autocommit_by_default
                else self._engine.execution_options(isolation_level="AUTOCOMMIT")
            )

            self.native_backend = build_native_backend(
                dialect=self._engine.dialect, enabled=self._native_execution
            )

            # Set up SQLite foreign keys pragma if using SQLite
            if self.rules.foreign_keys_pragma:  # pragma: nocover

                @event.listens_for(self._engine.sync_engine, "connect")
                def set_sqlite_pragma(dbapi_conn: Any, connection_record: Any) -> None:
                    cursor = dbapi_conn.cursor()
                    cursor.execute("PRAGMA foreign_keys=ON")
                    cursor.close()

            await self._open_min_connections()

            if self._force_rollback:
                assert self._global_transaction is None
                self._global_transaction = Transaction(
                    self, force_rollback=self._force_rollback
                )
                await self._global_transaction.__aenter__()

    async def _open_min_connections(self) -> None:
        """
        Open ``min_connections`` pooled connections at once and return them to
        the pool, capped at the pool size (connections beyond it are closed
        when returned).
        """
        assert self._engine is not None
        count = min(self._min_connections, self._options["pool_size"])
        if count > 0:
            async with AsyncExitStack() as stack:
                await asyncio.gather(
                    *(
                        stack.enter_async_context(self._engine.connect())
                        for _ in range(count)
                    )
                )

    async def disconnect(self) -> None:
        """
        Disconnect from the database and dispose of the engine.
        In case of force_rollback, also roll back the global transaction.
        """
        if self._engine is not None:
            if self._force_rollback:
                assert self._global_transaction is not None
                await self._global_transaction.__aexit__()
                self._global_transaction = None

            await self._engine.dispose()
            self._engine = None
            self._autocommit_engine = None
            self.native_backend = None

    @property
    def is_connected(self) -> bool:
        """Check if the engine is connected."""
        return self._engine is not None

    @property
    def engine(self) -> AsyncEngine:
        """Get the async engine."""
        assert self._engine is not None, "DatabaseConnection not connected"
        return self._engine

    @property
    def dialect(self) -> Any:
        """Get the database dialect."""
        return self.engine.dialect

    @property
    def url(self) -> str:
        """Get the database URL."""
        return self._url

    @asynccontextmanager
    async def connection(self) -> AsyncIterator[AsyncConnection]:
        """
        Get a connection from the pool.
        If inside a transaction, returns the transaction connection.
        """
        trans_conn = _transaction_connection.get()
        if trans_conn is not None:
            yield trans_conn
        else:
            async with self.engine.connect() as conn:
                await self.restore_transaction_isolation(conn)
                yield conn

    async def restore_transaction_isolation(self, conn: AsyncConnection) -> None:
        """
        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.

        :param conn: connection checked out from the main engine
        :type conn: AsyncConnection
        """
        if self._autocommit_by_default:  # pragma: no cover
            await conn.execution_options(
                isolation_level=conn.dialect.default_isolation_level
            )

    def transaction(self, force_rollback: bool = False) -> Transaction:
        """
        Create a transaction context manager.

        :param force_rollback: If True, always rollback (used for testing)
        """
        return Transaction(self, force_rollback=force_rollback)

    @asynccontextmanager
    async def get_query_executor(
        self, *, transactional: bool = False
    ) -> AsyncIterator[QueryExecutor]:
        """
        Get connection, reusing transaction connection if in transaction.

        :param transactional: If True, open an explicit transaction for the
            scope of the executor. Use for server-side cursors (``iterate``)
            and multi-row ``execute_many`` batches, which need a real
            transaction either because AUTOCOMMIT would commit each row
            separately or because the driver (asyncpg) requires a
            transaction for streaming.
        :type transactional: bool
        :return: QueryExecutor wrapping a connection
        :rtype: QueryExecutor
        """
        trans_conn = self.get_transaction_connection()
        if trans_conn is not None:
            # Inside a transaction - reuse the transaction's connection
            yield QueryExecutor(connection=trans_conn, native=self.native_backend)
        elif transactional:
            async with self.transaction():
                conn = self.get_transaction_connection()
                assert conn is not None
                yield QueryExecutor(connection=conn, native=self.native_backend)
        else:
            # Outside transaction: use AUTOCOMMIT view so each statement
            # commits at the driver level with no extra BEGIN/COMMIT
            # round-trip.
            assert self._autocommit_engine is not None
            async with self._autocommit_engine.connect() as conn:
                yield QueryExecutor(connection=conn, native=self.native_backend)

    @property
    def atomic_execute_many(self) -> bool:
        """
        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.

        :return: True when a lone batch needs no transaction
        :rtype: bool
        """
        return (
            self.native_backend is not None
            and self.native_backend.adapter.atomic_execute_many
        )

    async def start_driver_transaction(self, connection: AsyncConnection) -> None:
        """
        Make sure the driver-level transaction is open on a connection
        SQLAlchemy began a transaction on, so natively executed statements
        run inside it.

        :param connection: connection with a begun transaction
        :type connection: AsyncConnection
        """
        if self.native_backend is not None:
            await self.native_backend.adapter.start_transaction(
                sa_connection=connection
            )

    def get_transaction_connection(self) -> Optional[AsyncConnection]:
        """Get the current transaction connection if in a transaction."""
        return _transaction_connection.get()

    def set_transaction_connection(self, conn: Optional[AsyncConnection]) -> None:
        """Set the current transaction connection."""
        _transaction_connection.set(conn)

    async def __aenter__(self) -> "DatabaseConnection":
        """Async context manager entry - connect to a database."""
        await self.connect()
        return self

    async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
        """Async context manager exit - disconnect from database."""
        await self.disconnect()

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
async def __aenter__(self) -> "DatabaseConnection":
    """Async context manager entry - connect to a database."""
    await self.connect()
    return self

__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
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
    """Async context manager exit - disconnect from database."""
    await self.disconnect()

__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 create_async_engine; pool_size defaults to 10 and max_overflow to 10. min_connections (default pool_size) opens that many pooled connections on connect() (at most pool_size), so the first concurrent queries do not wait for new connections; 0 keeps the pool lazy. json_serializer and json_deserializer of JSON columns default to orjson.

{}
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
def __init__(self, url: str, native_execution: bool = True, **options: Any) -> None:
    """
    Initialize database connection.

    :param url: Database URL with an async driver (e.g., postgresql+asyncpg://)
    :param native_execution: run statements directly on the driver for
        asyncpg, asyncmy and aiosqlite instead of through SQLAlchemy's
        execution layer
    :param options: Additional engine options passed to
        ``create_async_engine``; ``pool_size`` defaults to 10 and
        ``max_overflow`` to 10. ``min_connections`` (default
        ``pool_size``) opens that many pooled connections on
        ``connect()`` (at most ``pool_size``), so the first concurrent
        queries do not wait for new connections; 0 keeps the pool lazy.
        ``json_serializer`` and ``json_deserializer`` of JSON columns
        default to orjson.
    """
    self._force_rollback = options.pop("force_rollback", False)
    self._url = url
    # Set reasonable pool defaults if not provided; overflow connections are
    # closed on checkin, so pool_size should cover the usual concurrency
    if "pool_size" not in options:
        options["pool_size"] = 10
    if "max_overflow" not in options:
        options["max_overflow"] = 10
    options.setdefault("json_serializer", serialize_json_column)
    options.setdefault("json_deserializer", deserialize_json_column)
    self._min_connections = options.pop("min_connections", options["pool_size"])
    self._options = options
    self._engine: Optional[AsyncEngine] = None
    self._autocommit_engine: Optional[AsyncEngine] = None
    self.rules: DialectRules = rules_for(
        backend_name=make_url(url).get_backend_name()
    )
    self._autocommit_by_default = (
        "isolation_level" not in options and self.rules.autocommit_engine
    )

    self._global_transaction: Optional[Transaction] = None
    self._native_execution = native_execution
    self.native_backend: Optional[NativeBackend] = None

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
async def connect(self) -> None:
    """Connect to the database by creating the async engine."""
    if self._engine is None:
        engine_options = (
            {**self._options, "isolation_level": "AUTOCOMMIT"}
            if self._autocommit_by_default
            else self._options
        )
        self._engine = create_async_engine(self._url, **engine_options)
        # Engine (or a view of the same engine/pool) in AUTOCOMMIT mode.
        # Standalone queries use this to avoid a BEGIN/COMMIT round-trip
        # per call, matching legacy `databases`-library semantics. Explicit
        # Transactions keep using ``self._engine`` so BEGIN / SAVEPOINT
        # still work.
        self._autocommit_engine = (
            self._engine
            if self._autocommit_by_default
            else self._engine.execution_options(isolation_level="AUTOCOMMIT")
        )

        self.native_backend = build_native_backend(
            dialect=self._engine.dialect, enabled=self._native_execution
        )

        # Set up SQLite foreign keys pragma if using SQLite
        if self.rules.foreign_keys_pragma:  # pragma: nocover

            @event.listens_for(self._engine.sync_engine, "connect")
            def set_sqlite_pragma(dbapi_conn: Any, connection_record: Any) -> None:
                cursor = dbapi_conn.cursor()
                cursor.execute("PRAGMA foreign_keys=ON")
                cursor.close()

        await self._open_min_connections()

        if self._force_rollback:
            assert self._global_transaction is None
            self._global_transaction = Transaction(
                self, force_rollback=self._force_rollback
            )
            await self._global_transaction.__aenter__()

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
@asynccontextmanager
async def connection(self) -> AsyncIterator[AsyncConnection]:
    """
    Get a connection from the pool.
    If inside a transaction, returns the transaction connection.
    """
    trans_conn = _transaction_connection.get()
    if trans_conn is not None:
        yield trans_conn
    else:
        async with self.engine.connect() as conn:
            await self.restore_transaction_isolation(conn)
            yield conn

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
async def disconnect(self) -> None:
    """
    Disconnect from the database and dispose of the engine.
    In case of force_rollback, also roll back the global transaction.
    """
    if self._engine is not None:
        if self._force_rollback:
            assert self._global_transaction is not None
            await self._global_transaction.__aexit__()
            self._global_transaction = None

        await self._engine.dispose()
        self._engine = None
        self._autocommit_engine = None
        self.native_backend = None

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 (iterate) and multi-row execute_many batches, which need a real transaction either because AUTOCOMMIT would commit each row separately or because the driver (asyncpg) requires a transaction for streaming.

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
@asynccontextmanager
async def get_query_executor(
    self, *, transactional: bool = False
) -> AsyncIterator[QueryExecutor]:
    """
    Get connection, reusing transaction connection if in transaction.

    :param transactional: If True, open an explicit transaction for the
        scope of the executor. Use for server-side cursors (``iterate``)
        and multi-row ``execute_many`` batches, which need a real
        transaction either because AUTOCOMMIT would commit each row
        separately or because the driver (asyncpg) requires a
        transaction for streaming.
    :type transactional: bool
    :return: QueryExecutor wrapping a connection
    :rtype: QueryExecutor
    """
    trans_conn = self.get_transaction_connection()
    if trans_conn is not None:
        # Inside a transaction - reuse the transaction's connection
        yield QueryExecutor(connection=trans_conn, native=self.native_backend)
    elif transactional:
        async with self.transaction():
            conn = self.get_transaction_connection()
            assert conn is not None
            yield QueryExecutor(connection=conn, native=self.native_backend)
    else:
        # Outside transaction: use AUTOCOMMIT view so each statement
        # commits at the driver level with no extra BEGIN/COMMIT
        # round-trip.
        assert self._autocommit_engine is not None
        async with self._autocommit_engine.connect() as conn:
            yield QueryExecutor(connection=conn, native=self.native_backend)

get_transaction_connection()

Get the current transaction connection if in a transaction.

Source code in ormar/databases/connection.py
Python
274
275
276
def get_transaction_connection(self) -> Optional[AsyncConnection]:
    """Get the current transaction connection if in a transaction."""
    return _transaction_connection.get()

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
async def restore_transaction_isolation(self, conn: AsyncConnection) -> None:
    """
    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.

    :param conn: connection checked out from the main engine
    :type conn: AsyncConnection
    """
    if self._autocommit_by_default:  # pragma: no cover
        await conn.execution_options(
            isolation_level=conn.dialect.default_isolation_level
        )

set_transaction_connection(conn)

Set the current transaction connection.

Source code in ormar/databases/connection.py
Python
278
279
280
def set_transaction_connection(self, conn: Optional[AsyncConnection]) -> None:
    """Set the current transaction connection."""
    _transaction_connection.set(conn)

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
async def start_driver_transaction(self, connection: AsyncConnection) -> None:
    """
    Make sure the driver-level transaction is open on a connection
    SQLAlchemy began a transaction on, so natively executed statements
    run inside it.

    :param connection: connection with a begun transaction
    :type connection: AsyncConnection
    """
    if self.native_backend is not None:
        await self.native_backend.adapter.start_transaction(
            sa_connection=connection
        )

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
def transaction(self, force_rollback: bool = False) -> Transaction:
    """
    Create a transaction context manager.

    :param force_rollback: If True, always rollback (used for testing)
    """
    return Transaction(self, force_rollback=force_rollback)

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
def build_native_backend(dialect: Dialect, enabled: bool) -> Optional[NativeBackend]:
    """
    Build the native backend for the dialect's driver.

    :param dialect: dialect of the connected database
    :type dialect: Dialect
    :param enabled: whether native execution is enabled
    :type enabled: bool
    :return: native backend, or None when disabled or the driver has no adapter
    :rtype: Optional[NativeBackend]
    """
    adapter = select_adapter(dialect=dialect) if enabled else None
    if adapter is None:
        return None
    return NativeBackend(
        dialect=dialect, adapter=adapter, compiler=NativeCompiler(dialect=dialect)
    )