dosubot[bot] commented on issue #35594: URL: https://github.com/apache/superset/issues/35594#issuecomment-3395632860
<!-- Answer --> It's technically possible to open a new SQLAlchemy session inside your event listener to log user management actions, which can avoid the "transaction is closed" error you see when using Superset's main session during flush/commit phases ([DBEventLogger code](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/utils/log.py)). For example, you could do something like: ```python from sqlalchemy.orm import sessionmaker from superset import db from superset.models.core import Log def log_user_event(mapper, connection, target): Session = sessionmaker(bind=db.engine) session = Session() try: log = Log( action="user_update", user_id=target.id, json='{"username": "%s"}' % target.username, ) session.add(log) session.commit() finally: session.close() ``` However, this approach is not officially recommended by Superset and comes with risks: you may run into transactional boundary issues, deadlocks, or data loss if the main transaction fails but your log is already committed. You should thoroughly test and monitor this if you go this route ([core model code](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/models/core.py)). The upgrade-safe and supported pattern is to use hooks on Flask AppBuilder views (like `after_model_change`), which run after the transaction is committed and are designed for side-effect logging. If you stick with event listeners and a new session, keep your implementation isolated and be aware of the risks. <!-- 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/76f009fc-300e-40f3-92ea-88e4ba634d35?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/76f009fc-300e-40f3-92ea-88e4ba634d35?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/76f009fc-300e-40f3-92ea-88e4ba634d35?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/76f009fc-300e-40f3-92ea-88e4ba634d35?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/76f009fc-300e-40f3-92ea-88e4ba634d35?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/76f009fc-300e-40f3-92ea-88e4ba634d35?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/76f009fc-300e-40f3-92ea-88e4ba634d35?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]
