Open JRMNfrtcm opened 5 months ago
🤖
Based on the code snippet you've shared, it seems like you're correctly using the LangChain and LangChain-Experimental libraries. The issue you're experiencing with SQL queries remaining active and pending on the server even after the answer is returned to the user might be due to the connections not being properly closed after the execution of the queries.
In the LangChain library, the connection to the database is established using the with self._engine.begin() as connection:
statement in the _execute
method of the SQLDatabase
class. This is a context manager that should automatically close the connection once the block of code is exited, even if an error occurs within the block. This ensures that the connection is always properly closed after the query is executed.
However, if an exception occurs within the with
block, the connection may not be properly closed. To ensure that the connections are always closed, even when an exception occurs, you could use a try/finally
block. Here is an example of how you can modify the _execute
method to ensure that the connection is always closed:
def _execute(
self,
command: Union[str, Executable],
fetch: Literal["all", "one", "cursor"] = "all",
*,
parameters: Optional[Dict[str, Any]] = None,
execution_options: Optional[Dict[str, Any]] = None,
) -> Union[Sequence[Dict[str, Any]], Result]:
"""
Executes SQL command through underlying engine.
If the statement returns no rows, an empty list is returned.
"""
parameters = parameters or {}
execution_options = execution_options or {}
connection = None
try:
connection = self._engine.begin() # type: Connection # type: ignore[name-defined]
# ... rest of the code ...
finally:
if connection is not None:
connection.close()
This modification ensures that the connection is always closed, even if an exception occurs within the try
block. This should prevent the SQL queries from remaining active and pending on the server after the answer is returned to the user.
Please note that modifying the _execute
method in this way might not be the best solution, as it could potentially interfere with other parts of the LangChain library that rely on the existing behavior of the _execute
method. It would be best to first try to identify the source of the exceptions that are causing the connections to not be properly closed, and address those issues directly.
If you continue to experience this issue, it would be helpful to have more information about the exceptions that are occurring. This could help in identifying the source of the problem and finding a more targeted solution.
It's very difficult to help without you specifying the SQL engine that you are using?
Hi @keenborder786 !
SQLCONNECTION= "mssql+pyodbc://USER:PASSWORD@SERVER:1433/DATABASENAME?&driver=ODBC+Driver+18+for+SQL+Server&Encrypt=yes&TrustServerCertificate=yes&Authentication=SqlPassword"
SQLAlchemy==2.0.27
Thanks!
Any update about this?
Anyone with one update about this topic or any workaround? Thanks
any update?
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
N/A
Description
Each time a question is asked, the SQL query is created and remains pending on the server even after the answer is returned to the user. The SQL query is only discarded after killing the console where the script runs.
System Info
langchain==0.1.7 langchain-experimental==0.0.51
Windows 11 Pro
Python 3.11.4