Skip to content

Session

Derevtsov Konstantin edited this page Jul 10, 2020 · 5 revisions

Session is an abstraction over logical transaction. Logical transactions incapsulate all changes that occurred in the domain area under one use case.

For example, in rest api, http endpoint provides access to use cases. So we need to create a session on every http request.

All interaction with storage occurs throughout the session. The lifecycle of a session is bounded by the beginning and end of a logical transaction. The main function of the session is to offer to create, read, and delete operations for instances of mapped entities.

For creating a session you can use Orm.MakeSession method.

    session := orm.MakeSession()

As you can see in examples, most D3 API's require not session, but context with the session inside. For creating a session and put it into context use Orm.CtxWithSession method.

    ctx := d3orm.CtxWithSession(context.Background())

For extract session from context use orm.Context function.

    session := orm.Session(ctx)
Flushing changes

After you complete with a logical transaction, you may want to commit all changes to the database, to do this use Flush method on a session.

    orm.Session(ctx).Flush()
Clone this wiki locally