Skip to content

Commit 8889740

Browse files
authored
fix: connection fixes (#2)
1 parent dac76e2 commit 8889740

File tree

5 files changed

+21
-23
lines changed

5 files changed

+21
-23
lines changed

examples/basic.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
from typing import TYPE_CHECKING
44

55
import msgspec
6-
from litestar import Controller, Litestar, get
7-
from litestar.exceptions import InternalServerException
6+
from litestar import Controller, Litestar, Request, get
87

98
from litestar_oracledb import AsyncDatabaseConfig, AsyncPoolConfig, OracleDatabasePlugin
109

@@ -18,14 +17,15 @@ class HealthCheck(msgspec.Struct):
1817

1918
class SampleController(Controller):
2019
@get(path="/sample")
21-
async def sample_route(self, db_connection: AsyncConnection) -> HealthCheck:
20+
async def sample_route(self, request: Request, db_connection: AsyncConnection) -> HealthCheck:
2221
"""Check database available and returns app config info."""
23-
cursor = db_connection.cursor()
24-
await cursor.execute("select 1 from dual")
25-
result = await cursor.fetchone()
26-
if result:
27-
return HealthCheck(status="online")
28-
raise InternalServerException
22+
with db_connection.cursor() as cursor:
23+
await cursor.execute("select 'a database value' a_column from dual")
24+
result = await cursor.fetchone()
25+
request.logger.info(result)
26+
if result:
27+
return HealthCheck(status="online")
28+
return HealthCheck(status="offline")
2929

3030

3131
oracledb = OracleDatabasePlugin(

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.0"
29+
version = "0.1.1"
3030

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

src/litestar_oracledb/config/_asyncio.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from litestar.constants import HTTP_RESPONSE_START
88
from litestar.exceptions import ImproperlyConfiguredException
9-
from litestar.types import Empty, EmptyType
109
from litestar.utils.dataclass import simple_asdict
1110
from oracledb import create_pool_async as oracledb_create_pool
1211
from oracledb.connection import AsyncConnection
@@ -26,7 +25,7 @@
2625

2726
from litestar import Litestar
2827
from litestar.datastructures.state import State
29-
from litestar.types import EmptyType, Message, Scope
28+
from litestar.types import Message, Scope
3029

3130

3231
def default_handler_maker(
@@ -122,7 +121,7 @@ class AsyncPoolConfig(GenericPoolConfig[AsyncConnectionPool, AsyncConnection]):
122121
class AsyncDatabaseConfig(GenericDatabaseConfig[AsyncConnectionPool, AsyncConnection]):
123122
"""Async Oracle database Configuration."""
124123

125-
pool_config: AsyncPoolConfig | None | EmptyType = Empty
124+
pool_config: AsyncPoolConfig | None = None
126125
"""Oracle Pool configuration"""
127126

128127
def __post_init__(self) -> None:
@@ -145,7 +144,7 @@ def pool_config_dict(self) -> dict[str, Any]:
145144
A string keyed dict of config kwargs for the Asyncpg :func:`create_pool <oracledb.pool.create_pool>`
146145
function.
147146
"""
148-
if self.pool_config is not None and self.pool_config != Empty:
147+
if self.pool_config is not None:
149148
return simple_asdict(self.pool_config, exclude_empty=True, convert_nested=False)
150149
msg = "'pool_config' methods can not be used when a 'pool_instance' is provided."
151150
raise ImproperlyConfiguredException(msg)
@@ -192,7 +191,7 @@ async def lifespan(
192191
try:
193192
yield
194193
finally:
195-
await db_pool.close(force=True)
194+
await db_pool.close()
196195

197196
async def provide_connection(
198197
self,

src/litestar_oracledb/config/_common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ class GenericDatabaseConfig(Generic[PoolT, ConnectionT]):
9797
9898
If set, the plugin will use the provided pool rather than instantiate one.
9999
"""
100-
before_send_handler: (
101-
BeforeMessageSendHookHandler | None | Literal["autocommit", "autocommit_include_redirects"] | EmptyType
102-
) = Empty
100+
before_send_handler: BeforeMessageSendHookHandler | None | Literal["autocommit", "autocommit_include_redirects"] = (
101+
None
102+
)
103103
"""Handler to call before the ASGI message is sent.
104104
105105
The handler should handle closing the session stored in the ASGI scope, if it's still open, and committing and

tests/test_plugin.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ async def test_lifespan(
1818
@get("/")
1919
async def health_check(db_connection: AsyncConnection) -> float:
2020
"""Check database available and returns random number."""
21-
cursor = db_connection.cursor()
22-
await cursor.execute("select 1 as the_one from dual")
23-
r = await cursor.fetchall()
24-
cursor.close()
25-
return r[0]["the_one"] # type: ignore
21+
with db_connection.cursor() as cursor:
22+
await cursor.execute("select 1 as the_one from dual")
23+
r = await cursor.fetchall()
24+
return r[0]["the_one"] # type: ignore
2625

2726
@asynccontextmanager
2827
async def lifespan(_app: Litestar) -> AsyncGenerator[None, Any]:

0 commit comments

Comments
 (0)