aminghadersohi commented on code in PR #38859:
URL: https://github.com/apache/superset/pull/38859#discussion_r2991334546


##########
superset/mcp_service/chart/tool/generate_chart.py:
##########
@@ -713,24 +714,25 @@ async def generate_chart(  # noqa: C901
         if request.save_chart and chart:
             from sqlalchemy.orm import joinedload
 
-            from superset.daos.chart import ChartDAO
+            from superset import db
             from superset.mcp_service.chart.schemas import 
serialize_chart_object
             from superset.models.slice import Slice
 
             # Re-fetch with eager-loaded relationships to avoid detached
             # instance errors when serialize_chart_object accesses .tags
-            # and .owners.  Use joinedload (single JOIN query) since we
-            # are fetching a single chart.
-            chart = (
-                ChartDAO.find_by_id(
-                    chart.id,
-                    query_options=[
-                        joinedload(Slice.owners),
-                        joinedload(Slice.tags),
-                    ],
-                )
-                or chart
-            )
+            # and .owners.  Use a direct db.session.query() instead of
+            # ChartDAO.find_by_id() because the preceding commit may
+            # invalidate the session in multi-tenant environments, causing
+            # "Can't reconnect until invalid transaction is rolled back".
+            try:
+                chart = (
+                    db.session.query(Slice)
+                    .options(joinedload(Slice.owners), joinedload(Slice.tags))
+                    .filter(Slice.id == chart.id)
+                    .first()
+                ) or chart
+            except SQLAlchemyError:
+                pass

Review Comment:
   Fixed in 18e85519d7. Added `db.session.rollback()` in the `except 
SQLAlchemyError` block for generate_chart.



##########
superset/mcp_service/dashboard/tool/add_chart_to_existing_dashboard.py:
##########
@@ -324,6 +324,7 @@ def add_chart_to_existing_dashboard(
     """
     try:
         from superset.commands.dashboard.update import UpdateDashboardCommand

Review Comment:
   Fixed in 18e85519d7. Moved `SQLAlchemyError` and `CommandException` imports 
before the `try` block so they're always bound when the `except` tuple is 
evaluated.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to