Skip to content

Commit b94a318

Browse files
committed
feat: check for connection before using
1 parent 8889740 commit b94a318

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,31 @@ from __future__ import annotations
4141

4242
from typing import TYPE_CHECKING
4343

44-
from litestar import Controller, Litestar, get
44+
from litestar import Controller, Litestar, Request, get
45+
4546
from litestar_oracledb import AsyncDatabaseConfig, AsyncPoolConfig, OracleDatabasePlugin
4647

4748
if TYPE_CHECKING:
4849
from oracledb import AsyncConnection
4950

5051

5152
class SampleController(Controller):
52-
@get(path="/sample")
53-
async def sample_route(self, db_connection: AsyncConnection) -> dict[str, str]:
53+
@get(path="/")
54+
async def sample_route(self, request: Request, db_connection: AsyncConnection) -> dict[str, str]:
5455
"""Check database available and returns app config info."""
55-
cursor = db_connection.cursor()
56-
await cursor.execute("select 1 from dual")
57-
result = await cursor.fetchone()
58-
return {"select_1": str(result)}
56+
with db_connection.cursor() as cursor:
57+
await cursor.execute("select 'a database value' a_column from dual")
58+
result = await cursor.fetchone()
59+
request.logger.info(result[0])
60+
if result:
61+
return {"a_column": result[0]}
62+
return {"a_column": "dunno"}
5963

6064

6165
oracledb = OracleDatabasePlugin(
62-
config=AsyncDatabaseConfig(pool_config=AsyncPoolConfig(user="app", password="super-secret", dsn="localhost:1521/freepdb1"))
66+
config=AsyncDatabaseConfig(
67+
pool_config=AsyncPoolConfig(user="system", password="super-secret", dsn="localhost:1513/FREEPDB1") # noqa: S106
68+
)
6369
)
6470
app = Litestar(plugins=[oracledb], route_handlers=[SampleController])
6571

examples/basic.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from typing import TYPE_CHECKING
44

5-
import msgspec
65
from litestar import Controller, Litestar, Request, get
76

87
from litestar_oracledb import AsyncDatabaseConfig, AsyncPoolConfig, OracleDatabasePlugin
@@ -11,26 +10,22 @@
1110
from oracledb import AsyncConnection
1211

1312

14-
class HealthCheck(msgspec.Struct):
15-
status: str
16-
17-
1813
class SampleController(Controller):
19-
@get(path="/sample")
20-
async def sample_route(self, request: Request, db_connection: AsyncConnection) -> HealthCheck:
14+
@get(path="/")
15+
async def sample_route(self, request: Request, db_connection: AsyncConnection) -> dict[str, str]:
2116
"""Check database available and returns app config info."""
2217
with db_connection.cursor() as cursor:
2318
await cursor.execute("select 'a database value' a_column from dual")
2419
result = await cursor.fetchone()
25-
request.logger.info(result)
20+
request.logger.info(result[0])
2621
if result:
27-
return HealthCheck(status="online")
28-
return HealthCheck(status="offline")
22+
return {"a_column": result[0]}
23+
return {"a_column": "dunno"}
2924

3025

3126
oracledb = OracleDatabasePlugin(
3227
config=AsyncDatabaseConfig(
33-
pool_config=AsyncPoolConfig(user="app", password="super-secret", dsn="localhost:1521/freepdb1") # noqa: S106
28+
pool_config=AsyncPoolConfig(user="system", password="super-secret", dsn="localhost:1513/FREEPDB1") # noqa: S106
3429
)
3530
)
3631
app = Litestar(plugins=[oracledb], route_handlers=[SampleController])

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.1"
29+
version = "0.1.2"
3030

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

src/litestar_oracledb/config/_asyncio.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ async def handler(message: Message, scope: Scope) -> None:
5050
None
5151
"""
5252
connection = cast("AsyncConnection | None", get_scope_state(scope, connection_scope_key))
53-
if connection and message["type"] in SESSION_TERMINUS_ASGI_EVENTS:
54-
await connection.close()
53+
if connection is not None and message["type"] in SESSION_TERMINUS_ASGI_EVENTS and connection._impl is not None: # noqa: SLF001
54+
# checks to to see if connected without raising an exception: https://github.com/oracle/python-oracledb/blob/main/src/oracledb/connection.py#L80
55+
if connection._impl is not None: # noqa: SLF001
56+
await connection.close()
5557
delete_scope_state(scope, connection_scope_key)
5658

5759
return handler
@@ -97,15 +99,20 @@ async def handler(message: Message, scope: Scope) -> None:
9799
"""
98100
connection = cast("AsyncConnection | None", get_scope_state(scope, connection_scope_key))
99101
try:
100-
if connection is not None and message["type"] == HTTP_RESPONSE_START:
102+
if connection is not None and message["type"] == HTTP_RESPONSE_START and connection._impl is not None: # noqa: SLF001
101103
if (message["status"] in commit_range or message["status"] in extra_commit_statuses) and message[
102104
"status"
103105
] not in extra_rollback_statuses:
104106
await connection.commit()
105107
else:
106108
await connection.rollback()
107109
finally:
108-
if connection and message["type"] in SESSION_TERMINUS_ASGI_EVENTS:
110+
# checks to to see if connected without raising an exception: https://github.com/oracle/python-oracledb/blob/main/src/oracledb/connection.py#L80
111+
if (
112+
connection is not None
113+
and message["type"] in SESSION_TERMINUS_ASGI_EVENTS
114+
and connection._impl is not None # noqa: SLF001
115+
):
109116
await connection.close()
110117
delete_scope_state(scope, connection_scope_key)
111118

src/litestar_oracledb/config/_sync.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ async def handler(message: Message, scope: Scope) -> None:
5151
None
5252
"""
5353
connection = cast("Connection | None", get_scope_state(scope, connection_scope_key))
54-
if connection and message["type"] in SESSION_TERMINUS_ASGI_EVENTS:
54+
# checks to to see if connected without raising an exception: https://github.com/oracle/python-oracledb/blob/main/src/oracledb/connection.py#L80
55+
if connection and message["type"] in SESSION_TERMINUS_ASGI_EVENTS and connection._impl is not None: # noqa: SLF001
5556
connection.close()
5657
delete_scope_state(scope, connection_scope_key)
5758

@@ -98,15 +99,15 @@ def handler(message: Message, scope: Scope) -> None:
9899
"""
99100
connection = cast("Connection | None", get_scope_state(scope, connection_scope_key))
100101
try:
101-
if connection is not None and message["type"] == HTTP_RESPONSE_START:
102+
if connection is not None and message["type"] == HTTP_RESPONSE_START and connection._impl is not None: # noqa: SLF001
102103
if (message["status"] in commit_range or message["status"] in extra_commit_statuses) and message[
103104
"status"
104105
] not in extra_rollback_statuses:
105106
connection.commit()
106107
else:
107108
connection.rollback()
108109
finally:
109-
if connection and message["type"] in SESSION_TERMINUS_ASGI_EVENTS:
110+
if connection and message["type"] in SESSION_TERMINUS_ASGI_EVENTS and connection._impl is not None: # noqa: SLF001
110111
connection.close()
111112
delete_scope_state(scope, connection_scope_key)
112113

0 commit comments

Comments
 (0)