Could anyone please help explain "expired object" in plain language?

24 views
Skip to first unread message

Bao Niu

unread,
Feb 23, 2014, 5:00:48 AM2/23/14
to sqlal...@googlegroups.com
I read the documentation several times yet still didn't find an official definition for "expired object", although it is used quite often. To my understanding, it means when you update some attributes on a persistent object, so those affected attributes that are still lying in database become "expired". Is my understanding correct, please? Thanks very much.

Gunnlaugur Thor Briem

unread,
Feb 23, 2014, 12:15:58 PM2/23/14
to sqlalchemy
It means that an object in memory (or some of its attributes), representing an entity in the DB, is no longer considered to reflect the state of that entity accurately because the entity may have changed in the DB. So next time attributes are read from the object, fresh DB state is queried. See http://docs.sqlalchemy.org/en/latest/orm/session.html#refreshing-expiring “To clear out the currently loaded state on an instance, the instance or its individual attributes can be marked as “expired”, which results in a reload to occur upon next access of any of the instance’s attrbutes.”

Gulli



On Sun, Feb 23, 2014 at 10:00 AM, Bao Niu <niub...@gmail.com> wrote:
I read the documentation several times yet still didn't find an official definition for "expired object", although it is used quite often. To my understanding, it means when you update some attributes on a persistent object, so those affected attributes that are still lying in database become "expired". Is my understanding correct, please? Thanks very much.

--
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 sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Michael Bayer

unread,
Feb 23, 2014, 4:34:56 PM2/23/14
to sqlal...@googlegroups.com
As our documentation ability is growing more sophisticated, I’ve taken advantage of this to update and expand the “Expiration” section of the documentation, including with some links to the outside regarding the important concepts.

I’m running out the door and the docs are still building, but if in 15 minutes you check out http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#refreshing-expiring the new content should be up (if the first word “expiring” isn’t hyperlinked, then the build didn’t work).  If those involved can proof it, assuming the build succeeds, that would be great, thanks !
signature.asc

Bao Niu

unread,
Feb 24, 2014, 7:06:51 AM2/24/14
to sqlal...@googlegroups.com

Sorry for my slowness, but I still have a minor problem understanding "expired object". Why does it always assume that the database carries the most recent and desired data so that the instances in memory are marked expired? Can't an "expiration" mean the other way around, in which the objects in memory are recent and the database is marked expired, so that next attribute access would automatically cause an update on the database according to the attributes on the objects currently in memory?

You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/tGGrkYX5tlE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.

Simon King

unread,
Feb 24, 2014, 7:25:08 AM2/24/14
to sqlal...@googlegroups.com
When you first load an object from the database, it is considered
"clean". When you then start to change its attributes, it becomes
"dirty". When you call session.commit(), all the dirty objects in the
session are written to the database.

However, it is possible that while you have been operating within your
transaction, other people have also been writing to the database, so
the objects that you have a reference to are no longer up-to-date. So
SQLAlchemy's default behaviour is to mark all attributes as "expired"
when you call session.commit(), so the next time you access one of
these attributes, the value will be loaded from the database.

Since expiration normally happens only when you commit, I don't think
the situation you are describing (where application state is newer
than DB state) should ever arise. If you think that's not the case,
perhaps you could describe a situation in which you have expired
objects whose state is newer than the database?

Cheers,

Simon

Jonathan Vanasco

unread,
Feb 24, 2014, 11:15:40 AM2/24/14
to sqlal...@googlegroups.com
To expand and clarify Simon's answer into bulletpoints :

Expired Object
* The database Session the object belongs to has been closed ( or committed unless you tweaked the config )
* SqlAlchemy considers it 'expired' because it is not reasonable to expect the object to reflect the current state of the database
* the object must be reloaded from the db or 'merged' into a new session.

Dirty Object
* There have been changes to attributes on a "Clean" object loaded from the database
* Changing an attribute , or adding an object, doesn't automatically write to the database.  
* You must explicitly call `flush()` to write the database, or `commit()` to both write to the database and commit the transaction.

Overall
* SqlAlchemy is your friend and looks out for you, avoiding common mistakes:
* It automatically reloads most "Expired Objects" from the database (and raises an error at other times) so you have the most correct data in the ORM
* It makes you explicitly write to the database when you have new objects or changes to commit.

While it could be possible to extend SqlAlchemy to automatically 'write' to the database when you change an attribute, that would cause a lot of SQL overhead and probably screw up constraint checks.






Bao Niu

unread,
Feb 24, 2014, 6:03:39 PM2/24/14
to sqlal...@googlegroups.com

Sqlalchemy is great! The more I explore, the more inner beauty of it I realize.

--
Reply all
Reply to author
Forward
0 new messages