>From looking at your code example, I don't see you calling
celery_session.flush() or celery_session.commit(), so nothing is going to
persist.  Just setting an attribute value does not send any SQL.

On Thu, Jun 21, 2018, 7:22 PM HP3 <[email protected]> wrote:

> Hello all:
>
> Within a celery task, I need to have 2 unrelated sessions that can
> commit/rollback independently of each other:
> One session (`domain_model_session`) performs vanilla domain model
> operations and the other (`celery_state_session`) is to persist the state
> of the celery task itself **isolated from the domain model one**.
>
> *Imagine "checkpoints"*
>
> Something along these lines
>
> @task
> def my task(...):
>
>            *# Persist checkpoint 'started'*
>
> cs = celery_state_session.query(CeleryTask).get(self.request.id)
> cs.started = datetime.now()
> celery_state_session.commit()
>
> try:
>
>
>
>     with transaction.manager:
>         mymodel = domain_model_session.query(MyModel).one()
>         mymodel.value = "..."
>
>                    *# Persist checkpoint 'value updated' *
>
>         cs = celery_state_session.query(CeleryTask).get(self.request.id)
>         cs.model_value_updated = datetime.now()
>
> except Exception as e:
>
>
>              * # Persist checkpoint 'transaction failed'  <<< This
> checkpoint is never persisted !!!!*
>
>    cs = celery_state_session.query(CeleryTask).get(self.request.id)
>    cs.failed_with_exception = datetime.now()
>
> finally:
>
>
>               *# Persist checkpoint 'celery task completed anyhow'  *
>
>    cs = celery_state_session.query(CeleryTask).get(self.request.id)
>    cs.failed_with_exception = datetime.now()
>
>
>
> I've been trying multiple things but I can't get the behavior I need.
>
> No matter what, whenever the transaction associated to
> `domain_model_session` fails it "rolls back" all operations performed by
> `celery_state_session` as if `celery_state_session` was automatically bound
> to the same transaction.manager.
>
> How am I supposed to create a session that works isolated?
>
>
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to