Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Gorm ¶
Gorm session implementation.
func (Gorm) Begin ¶
Begin returns a new session with the given context and a started DB transaction. The returned session has manual controls. Make sure a call to `Rollback()` or `Commit()` is executed before the session is expired (eligible for garbage collection). The Gorm DB associated with this session is injected as a value into the new session's context. If a Gorm DB is found in the given context, it will be used instead of this Session's DB, allowing for nested transactions.
func (Gorm) Context ¶
Context returns the session's context. If it's the root session, `context.Background()` is returned. If it's a child session started with `Begin()`, then the context will contain the associated Gorm DB and can be used in combination with `session.DB()`.
func (Gorm) Transaction ¶
Transaction executes a transaction. If the given function returns an error, the transaction is rolled back. Otherwise it is automatically committed before `Transaction()` returns.
The Gorm DB associated with this session is injected into the context as a value so `session.DB()` can be used to retrieve it.
type Session ¶
type Session interface { // Begin returns a new session with the given context and a started transaction. // Using the returned session should have no side-effect on the parent session. // The underlying transaction mechanism is injected as a value into the new session's context. Begin(ctx context.Context) (Session, error) // Transaction executes a transaction. If the given function returns an error, the transaction // is rolled back. Otherwise it is automatically committed before `Transaction()` returns. // The underlying transaction mechanism is injected into the context as a value. Transaction(ctx context.Context, f func(context.Context) error) error // Rollback the changes in the transaction. This action is final. Rollback() error // Commit the changes in the transaction. This action is final. Commit() error // Context returns the session's context. If it's the root session, `context.Background()` is returned. // If it's a child session started with `Begin()`, then the context will contain the associated // transaction mechanism as a value. Context() context.Context }
Session aims at facilitating business transactions while abstracting the underlying mechanism, be it a database transaction or another transaction mechanism. This allows services to execute multiple business use-cases and easily rollback changes in case of error, without creating a dependency to the database layer.
Sessions should be constituted of a root session created with a "New"-type constructor and allow the creation of child sessions with `Begin()` and `Transaction()`. Nested transactions should be supported as well.