Hi.

Imagine the following scenario:



session = DBSession()
readonly_model = session.query(ReadOnlyModel).get(id)

# Readonly means the model will NOT have its data changed in the life of the transaction(s).

method_one(readonly_model.readonly_data, param_1, param_2, ...)
method_two(readonly_model.readonly_data, param_3, param_4, ...)

session.commit()



Now, the code here is the "caller" and the methods one and two are logically separate, meaning they do not know anything about the caller, nor the caller knows anything about the internal states and processing involved in the methods.

Suppose a transaction fails in one or both methods, say an IntegrityError. The methods know how to handle this, catch the exceptions and adapt accordingly. They manually do session.rollback() and proceed within new transaction.

The problem is, if this happens in method_one, then readonly_model is expired and has no readonly_model.readonly_data when method two is called. From what I've read in the docs, I am supposed to do session.refresh(), but that means the caller must know what happened within the methods one and two (rollback happened).

One way would be to extract data from the readonly_model:

readonly_data = readonly_model.readonly_data

method_one(readonly_data, param_1, param_2, ...)
...

Now, I am certain that this readonly data will NOT change between two transactions, 100% sure it will never happen in the life of entire request / all transactions involved in the process, so basically extracting it from the session/model is safe, within the logic of the code.

Any suggestions/advices about this? Am I doing something completely wrong? What if the readonly_data was not 100% sure not to change? Is my only option to have the caller somehow know there was rollback involved and refresh the session?


Thanks!

--

.oO V Oo.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to