Search code examples
c#postgresqlnosqlmarten

Marten OpenSession() vs LightweightSession()


I am new to marten.
what is difference between IDocumentStore.OpenSession() vs IDocumentStore.LightweightSession()?


Solution

  • Based on the xml doc it is just a convenience method:

    Convenience method to create a new "lightweight" IDocumentSession with no IdentityMap or automatic dirty checking

    In the current implementation it just calls OpenSession with DocumentTracking set to DocumentTracking.None.

    Also from the troubleshooting docs:

    From

    IDocumentStore.OpenSession

    Characteristics:

    Defaults to session that tracks objects by their identity, DocumentTracking.IdentityOnly, with isolation level of Read Committed.

    Use: Reading & writing data. Objects within a session are cached by their identity. Updates to objects are explicitly controlled through session operations (IDocumentSession.Update, IDocumentSession.Store). With the defaults, incurs lower overhead than DirtyTrackedSession.

    And

    From

    IDocumentStore.LightweightSession

    Characteristics:

    No change tracking, DocumentTracking.None, with the default isolation level of Read Committed.

    Use: Reading & writing data. No caching of objects is done within a session, e.g. repeated loads using the same document identity yield separate objects, each hydrated from the database. In case of updates to such objects, the last object to be stored will overwrite any pending changes from previously stored objects of the same identity. Can incur lower overhead than tracked sessions.