dosubot[bot] commented on issue #35594: URL: https://github.com/apache/superset/issues/35594#issuecomment-3395602025
<!-- Answer --> The error happens because you're trying to write to Superset's logs table from inside SQLAlchemy event listeners (like after_insert, after_update, after_delete), which run during the flush/commit phase of a transaction. At that point, the session may be in a closed or flushing state, so any new database writes (like logging) can trigger "This transaction is closed" errors [see code](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/utils/log.py). The recommended pattern is to avoid logging to the database directly from model event listeners. Instead, use hooks provided by Flask AppBuilder (FAB) views, such as `after_model_change`, `after_model_create`, or `after_model_delete`, which are called after the transaction is safely committed. These hooks are designed for side effects like auditing and logging, and operate in a stable transaction context [see note](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/views/base.py). If you need to log user management actions, subclass the relevant FAB ModelView (for example, UserView), override the appropriate hook, and call your logger there. This approach avoids transaction errors and ensures the request context (for IP address, actor, etc.) is available. If you want a concrete example for patching the view logic or using FAB hooks, let me know! <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/4e5a7760-9439-48f6-89fc-ada8f4e7c3af?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/4e5a7760-9439-48f6-89fc-ada8f4e7c3af?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/4e5a7760-9439-48f6-89fc-ada8f4e7c3af?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/4e5a7760-9439-48f6-89fc-ada8f4e7c3af?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/4e5a7760-9439-48f6-89fc-ada8f4e7c3af?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/4e5a7760-9439-48f6-89fc-ada8f4e7c3af?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/4e5a7760-9439-48f6-89fc-ada8f4e7c3af?feedback_type=other)</sup> [](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=githu b) [](https://go.dosu.dev/discord-bot) [](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/issues/35594) -- 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]
