Skip to content

Commit b665d1a

Browse files
author
junjie.miao
committed
'DbUtil' support the context manager protocol
1 parent 84ef3ad commit b665d1a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

db_query/tools/db_util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ def __init__(self, db_type: str,
2929
oracledb.init_oracle_client()
3030
self.engine = create_engine(self.get_url(), pool_size=100, pool_recycle=36)
3131

32+
def __enter__(self):
33+
return self
34+
35+
def __exit__(self, exc_type, exc_val, exc_tb):
36+
self.close()
37+
3238
def get_driver_name(self):
3339
driver_name = self.db_type
3440
if self.db_type == 'mysql':
@@ -60,6 +66,10 @@ def get_url(self):
6066
logging.info(f"url: {url}")
6167
return url
6268

69+
def close(self):
70+
"""Close all connections in the engine."""
71+
self.engine.dispose()
72+
6373
def run_query(self, query_sql: str) -> list[dict]:
6474
'''
6575
Run SQL Query

db_query_pre_auth/tools/db_util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ def __init__(self, db_type: str,
2929
oracledb.init_oracle_client()
3030
self.engine = create_engine(self.get_url(), pool_size=100, pool_recycle=36)
3131

32+
def __enter__(self):
33+
return self
34+
35+
def __exit__(self, exc_type, exc_val, exc_tb):
36+
self.close()
37+
3238
def get_driver_name(self):
3339
driver_name = self.db_type
3440
if self.db_type == 'mysql':
@@ -60,6 +66,10 @@ def get_url(self):
6066
logging.info(f"url: {url}")
6167
return url
6268

69+
def close(self):
70+
"""Close all connections in the engine."""
71+
self.engine.dispose()
72+
6373
def run_query(self, query_sql: str) -> list[dict]:
6474
'''
6575
Run SQL Query

0 commit comments

Comments
 (0)