Skip to content

Commit e3de7ba

Browse files
author
junjie.miao
committed
Optimize the resource release of SQL query execution
1 parent b665d1a commit e3de7ba

File tree

3 files changed

+16
-30
lines changed

3 files changed

+16
-30
lines changed

db_query/tools/sql_query.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,14 @@ def _invoke(
4747
output_format = tool_parameters.get("output_format", "markdown").lower()
4848

4949
try:
50-
db = DbUtil(db_type=db_type,
50+
with DbUtil(db_type=db_type,
5151
username=db_username, password=db_password,
5252
host=db_host, port=db_port,
53-
database=db_name, properties=db_properties)
53+
database=db_name, properties=db_properties) as db:
54+
records = db.run_query(query_sql)
5455
except Exception as e:
55-
message = "Database connection creation exception."
56-
logging.exception(message)
57-
raise Exception(message + " {}".format(e))
58-
59-
try:
60-
records = db.run_query(query_sql)
61-
except Exception as e:
62-
message = "SQL query execution exception."
63-
logging.exception(message)
64-
raise Exception(message + " {}".format(e))
56+
logging.exception("SQL query execution failed: %s", str(e))
57+
raise RuntimeError(f"Error executing SQL: {e}") from e
6558

6659
if output_format == "json":
6760
yield self.create_json_message({"records": records})

db_query_pre_auth/provider/db_query.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ def _validate_credentials(self, credentials: dict) -> None:
2424
raise ValueError("Database port can be empty or fill with integer value")
2525
db_name = credentials.get("db_name", "")
2626
db_properties = credentials.get("db_properties", "")
27+
2728
try:
28-
db = DbUtil(db_type=db_type,
29+
with DbUtil(db_type=db_type,
2930
username=db_username, password=db_password,
3031
host=db_host, port=db_port,
31-
database=db_name, properties=db_properties)
32-
db.run_query(db.test_sql())
32+
database=db_name, properties=db_properties) as db:
33+
db.run_query(db.test_sql())
3334
except Exception as e:
34-
message = "Database connection failure."
35-
logging.exception(message)
36-
raise Exception(message + " {}".format(e))
35+
logging.exception("Database connection failed: %s", str(e))
36+
raise RuntimeError(f"Error connection database: {e}") from e

db_query_pre_auth/tools/sql_query.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,14 @@ def _invoke(
4747
output_format = tool_parameters.get("output_format", "markdown").lower()
4848

4949
try:
50-
db = DbUtil(db_type=db_type,
50+
with DbUtil(db_type=db_type,
5151
username=db_username, password=db_password,
5252
host=db_host, port=db_port,
53-
database=db_name, properties=db_properties)
53+
database=db_name, properties=db_properties) as db:
54+
records = db.run_query(query_sql)
5455
except Exception as e:
55-
message = "Database connection creation exception."
56-
logging.exception(message)
57-
raise Exception(message + " {}".format(e))
58-
59-
try:
60-
records = db.run_query(query_sql)
61-
except Exception as e:
62-
message = "SQL query execution exception."
63-
logging.exception(message)
64-
raise Exception(message + " {}".format(e))
56+
logging.exception("SQL query execution failed: %s", str(e))
57+
raise RuntimeError(f"Error executing SQL: {e}") from e
6558

6659
if output_format == "json":
6760
yield self.create_json_message({"records": records})

0 commit comments

Comments
 (0)