go-transaction-manager
101 строка · 3.7 Кб
1// Package trm contains of interfaces to programmatic transaction management.
2package trm
3
4//go:generate mockgen -source=$GOFILE -destination=drivers/mock/$GOFILE -package=mock
5
6import (
7"context"
8"errors"
9"fmt"
10)
11
12var (
13// ErrTransaction is an error while working with a transaction.
14ErrTransaction = errors.New("transaction")
15// ErrAlreadyClosed occurs if the transaction was closed outside the Manager.
16ErrAlreadyClosed = errTransaction("already closed")
17
18// ErrBegin occurs when a transaction started with an error.
19ErrBegin = errTransaction("begin")
20// ErrCommit occurs when commit finished with an error.
21ErrCommit = errTransaction("commit")
22// ErrRollback occurs when rollback finished with an error.
23ErrRollback = errTransaction("rollback")
24
25// ErrNestedBegin occurs when nested transaction started with an error.
26ErrNestedBegin = errNested(ErrBegin, "nested")
27// ErrNestedCommit occurs when nested transaction finished with an error.
28ErrNestedCommit = errNested(ErrCommit, "nested")
29// ErrNestedRollback occurs when rollback nested transaction finished with an error.
30ErrNestedRollback = errNested(ErrRollback, "nested")
31)
32
33// TrFactory is used in Manager to creates Transaction.
34type TrFactory func(ctx context.Context, s Settings) (context.Context, Transaction, error)
35
36// NestedTrFactory creates nested Transaction.
37type NestedTrFactory interface {
38Begin(ctx context.Context, s Settings) (context.Context, Transaction, error)
39}
40
41// Transaction wraps different transaction implementations.
42type Transaction interface {
43// Transaction returns the real transaction sql.Tx, sqlx.Tx or another.
44Transaction() interface{}
45// Commit the trm.Transaction.
46// Commit should be used only inside of Manager.
47Commit(context.Context) error
48// Rollback the trm.Transaction.
49// Rollback should be used only inside of Manager.
50Rollback(context.Context) error
51// IsActive returns true if the transaction started but not committed or rolled back.
52IsActive() bool
53// Closed returns a channel that's closed when transaction committed or rolled back.
54Closed() <-chan struct{}
55}
56
57// transactionWithSP is used for tests to generate mock.
58//
59//nolint:unused
60type transactionWithSP interface {
61Transaction
62NestedTrFactory
63}
64
65var (
66// ErrPropagation occurs because of Propagation setting.
67ErrPropagation = errTransaction("propagation")
68// ErrPropagationMandatory occurs when the transaction doesn't exist.
69ErrPropagationMandatory = errNested(ErrPropagation, "mandatory")
70// ErrPropagationNever occurs when the transaction already exists.
71ErrPropagationNever = errNested(ErrPropagation, "never")
72)
73
74// Propagation is a type for transaction propagation rules.
75type Propagation int8
76
77const (
78// PropagationRequired supports a current transaction, create a new one if none exists. This is default setting.
79PropagationRequired Propagation = iota
80// PropagationNested executes within a nested transaction
81// if a current transaction exists, create a new one if none exists.
82PropagationNested
83// PropagationsMandatory supports a current transaction, throws an exception if none exists.
84PropagationsMandatory
85// PropagationNever executes non-transactionally, throws an exception if a transaction exists.
86PropagationNever
87// PropagationNotSupported executes non-transactionally, suspends the current transaction if one exists.
88PropagationNotSupported
89// PropagationRequiresNew creates a new transaction, suspends the current transaction if one exists.
90PropagationRequiresNew
91// PropagationSupports supports a current transaction, execute non-transactionally if none exists.
92PropagationSupports
93)
94
95func errNested(err error, msg string) error {
96return fmt.Errorf("%w: %s", err, msg)
97}
98
99func errTransaction(msg string) error {
100return errNested(ErrTransaction, msg)
101}
102