Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ContextAccessor ¶
type ContextAccessor interface {
FromContext(ctx context.Context) (tx interface{}, sf StepFinalizer, err error)
}
type ContextAccessorFunc ¶
type ContextAccessorFunc func(ctx context.Context) (tx interface{}, sf StepFinalizer, err error)
func (ContextAccessorFunc) FromContext ¶
func (fn ContextAccessorFunc) FromContext(ctx context.Context) (tx interface{}, sf StepFinalizer, err error)
type MultiError ¶
type MultiError struct {
Errors []error
}
func (MultiError) Error ¶
func (err MultiError) Error() string
type OnePhaseCommitAdapter ¶
type OnePhaseCommitAdapter interface { // BeginTx initiates a transaction block, that is, // all statements after a BEGIN command will be executed in a single transaction // until an explicit COMMIT or ROLLBACK is given. BeginTx(ctx context.Context) (tx interface{}, err error) // Commit commits the current transaction. // All changes made by the transaction become visible to others and are guaranteed to be durable if a crash occurs. Commit(tx interface{}) error // Rollback rolls back the current transaction and causes all the updates made by the transaction to be discarded. Rollback(tx interface{}) error }
type OnePhaseCommitTxManager ¶
type OnePhaseCommitTxManager struct {
// contains filtered or unexported fields
}
OnePhaseCommitTxManager serves as a context based transaction manager to solve the problem of having a resource across many different supplier implementations in one transaction.
e.g.: many function works implements different goals on top of a database, and the they should act like one unit during an ineractor call.
OnePhaseCommitTxManager does not guarantee that handled resources act together, in case one commit fails, there is no guarantee that units will be rolled back. handled transactions interpreted as independent resources. For linking commit/rollback lifecycle management in resources into one unit, see TwoPhaseCommitTxManager.
func NewOnePhaseCommitTxManager ¶
func NewOnePhaseCommitTxManager() *OnePhaseCommitTxManager
Example ¶
package main import ( "context" "database/sql" "github.com/adamluzsi/frameless/fixtures" "github.com/adamluzsi/frameless/transactions" ) func main() error { // during init var txm = transactions.NewOnePhaseCommitTxManager() var supplier = NewOPCExampleExternalResourceSupplier(txm) // during request without context level tx management var ctxWithoutTxManagement = context.Background() if err := supplier.DoSomething(ctxWithoutTxManagement); err != nil { return err } // during request with context level tx management var ctxWithTxManagement = context.Background() ctxWithTxManagement, handler := txm.ContextWithTransactionManagement(ctxWithTxManagement) if err := supplier.DoSomething(ctxWithoutTxManagement); err != nil { return handler.Rollback() } return handler.Commit() } func NewOPCExampleExternalResourceSupplier(txm *transactions.OnePhaseCommitTxManager) *OPCExampleExternalResourceSupplier { return &OPCExampleExternalResourceSupplier{ContextAccessor: txm.RegisterAdapter(OPCETXAdapter{})} } type OPCExampleExternalResourceSupplier struct { ContextAccessor transactions.ContextAccessor } func (s *OPCExampleExternalResourceSupplier) DoSomething(ctx context.Context) (rErr error) { tx, sf, err := s.ContextAccessor.FromContext(ctx) if err != nil { return err } defer func() { dErr := sf.Done() if rErr == nil { rErr = dErr // pass tx finalization err as return value } }() if _, err := tx.(*OPCETx).ExecContext(ctx, `SELECT 1`); err != nil { return err } return nil } type OPCETx struct { Err error committed bool rolledback bool id string } func (tx *OPCETx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { return nil, nil } type OPCETXAdapter struct { } func (t OPCETXAdapter) BeginTx(context.Context) (ptr interface{}, err error) { return &OPCETx{id: fixtures.Random.String()}, nil } func (t OPCETXAdapter) Commit(ptr interface{}) error { ptr.(*OPCETx).committed = true return ptr.(*OPCETx).Err } func (t OPCETXAdapter) Rollback(ptr interface{}) error { ptr.(*OPCETx).rolledback = true return ptr.(*OPCETx).Err }
Output:
func (*OnePhaseCommitTxManager) ContextWithTransactionManagement ¶
func (*OnePhaseCommitTxManager) RegisterAdapter ¶
func (txm *OnePhaseCommitTxManager) RegisterAdapter(adapter OnePhaseCommitAdapter) ContextAccessor
type StepFinalizer ¶
type StepFinalizer interface {
Done() error
}
Source Files ¶
Click to show internal directories.
Click to hide internal directories.