pythonsqlalchemyidentity-map

sqlalchemy identity map question


The identity map and unit of work patterns are part of the reasons sqlalchemy is much more attractive than django.db. However, I am not sure how the identity map would work, or if it works when an application is configured as wsgi and the orm is accessed directly through api calls, instead of a shared service. I would imagine that apache would create a new thread with its own python instance for each request. Each instance would therefore have their own instance of the sqlalchemy classes and not be able to make use of the identity map. Is this correct?


Solution

  • I think you misunderstood the identity map pattern.

    From : http://martinfowler.com/eaaCatalog/identityMap.html

    An Identity Map keeps a record of all objects that have been read from the database in a single business transaction.

    Records are kept in the identity map for a single business transaction. This means that no matter how your web server is configured, you probably will not hold them for longer than a request (or store them in the session).

    Normally, you will not have many users taking part in a single business transation. Anyway, you probably don't want your users to share objects, as they might end up doing things that are contradictory.