Skip to content

Commit 0620dbb

Browse files
authored
feat: adds get_connection methods (#4)
1 parent b94a318 commit 0620dbb

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ license = { text = "MIT" }
2626
name = "litestar-oracledb"
2727
readme = "README.md"
2828
requires-python = ">=3.8"
29-
version = "0.1.2"
29+
version = "0.1.3"
3030

3131
[project.urls]
3232
Changelog = "https://litestar-org.github.io/litesatr-oracledb/latest/changelog"

src/litestar_oracledb/config/_asyncio.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,16 @@ async def provide_connection(
224224
async with pool.acquire() as connection:
225225
set_scope_state(scope, self.connection_scope_key, connection)
226226
yield connection
227+
228+
@asynccontextmanager
229+
async def get_connection(
230+
self,
231+
) -> AsyncGenerator[AsyncConnection, None]:
232+
"""Create a connection instance.
233+
234+
Returns:
235+
A connection instance.
236+
"""
237+
pool = await self.create_pool()
238+
async with pool.acquire() as connection:
239+
yield connection

src/litestar_oracledb/config/_sync.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from contextlib import asynccontextmanager
3+
from contextlib import asynccontextmanager, contextmanager
44
from dataclasses import dataclass
55
from typing import TYPE_CHECKING, Generator, Optional, cast
66

@@ -193,7 +193,7 @@ async def lifespan(
193193
try:
194194
yield
195195
finally:
196-
db_pool.close(force=True)
196+
db_pool.close()
197197

198198
def provide_connection(
199199
self,
@@ -219,3 +219,16 @@ def provide_connection(
219219
with pool.acquire() as connection:
220220
set_scope_state(scope, self.connection_scope_key, connection)
221221
yield connection
222+
223+
@contextmanager
224+
def get_connection(
225+
self,
226+
) -> Generator[Connection, None, None]:
227+
"""Create a connection instance.
228+
229+
Returns:
230+
A connection instance.
231+
"""
232+
pool = self.create_pool()
233+
with pool.acquire() as connection:
234+
yield connection

0 commit comments

Comments
 (0)