transaction_manager

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2020 License: MIT Imports: 9 Imported by: 0

README

Xorm-TransactionManager

xorm transaction-manager is the xorm automatic transaction manager. With this library, you don’t need to worry about global transaction.

Routine Transaction Manager

routine manager save session in the sync.map,in same go routine,DynamicSession function returns the same session

Context Transaction Manager

context manager save session in the context.Context,DynamicSession function will get the session in the context

Installation

go get github.com/RuiFG/xorm-transactionmanager 

Simple Example

Routine Manager

package main

import (
"context"
"github.com/RuiFG/xorm-transactionmanager"
"github.com/go-xorm/xorm"
)

func main() {
	DB, _ := xorm.NewEngine("mysql", "bp_ops:bp_ops@baishan.com@tcp(172.18.2.21:3306)/bp_ops?charset=utf8&parseTime=True&loc=Asia%2FShanghai")
    // use routine transaction manager
	manager := transaction_manager.NewRoutineTransactionManager(DB)
	err := manager.Do(context.Background(), func(ctx context.Context, session *xorm.Session) error {
		return manager.Do(ctx, func(ctx context.Context, session *xorm.Session) error {
			m := make([]int, 0)
			session, _ = manager.DynamicSessionFunc()(ctx)
			_ = session.Table("test").Cols("id").Find(&m)
			fmt.Println(m)
			return nil
		})
	})
	fmt.Print(err)
}

Context Manager

package main

import (
"context"
"fmt"
"github.com/RuiFG/xorm-transactionmanager"
"github.com/go-xorm/xorm"
)

func main() {
	DB, _ := xorm.NewEngine("mysql", "bp_ops:bp_ops@baishan.com@tcp(172.18.2.21:3306)/bp_ops?charset=utf8&parseTime=True&loc=Asia%2FShanghai")
    // use context transaction manager
	manager := transaction_manager.NewContextTransactionManager(DB)
	err := manager.Do(context.Background(), func(ctx context.Context, session *xorm.Session) error {
		return manager.Do(ctx, func(ctx context.Context, session *xorm.Session) error {
			m := make([]int, 0)
			session, _ = manager.DynamicSessionFunc()(ctx)
			_ = session.Table("test").Cols("id").Find(&m)
			fmt.Println(m)
			return nil
		})
	})
	fmt.Print(err)
}

License

This project is under MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrContextParameter               = errors.New("context transaction manager need context")
	ErrNeverNotSupported              = errors.New("never transaction definition not supported transaction")
	ErrUnSupportTransactionDefinition = errors.New("unsupported transaction")
	ErrMandatoryInTransaction         = errors.New("mandatory needs to be in the transaction")
)
View Source
var (
	MapError = errors.New("error loaded session in the map")
)

Functions

This section is empty.

Types

type ContextSessionKey

type ContextSessionKey struct{}

type DynamicSession

type DynamicSession func(...context.Context) (*Session, error)

return session of the transaction manager

type TransactionDefinition added in v0.1.0

type TransactionDefinition uint
const (
	PROPAGATION_REQUIRED TransactionDefinition
	PROPAGATION_SUPPORTS
	PROPAGATION_MANDATORY
	PROPAGATION_REQUIRES_NEW
	PROPAGATION_NOT_SUPPORTED
	PROPAGATION_NEVER
	PROPAGATION_NESTED
)

type TransactionFunc

type TransactionFunc func(context.Context, *Session) error

transaction function

type TransactionManager

type TransactionManager interface {
	//DynamicSessionFunc return a function
	DynamicSessionFunc() DynamicSession
	//IsInTransaction returns a value to determine whether it is currently in a transaction
	IsInTransaction(ctx context.Context) bool
	Do(context.Context, TransactionFunc, ...TransactionDefinition) error
	Required(context.Context, TransactionFunc) error
	Supports(context.Context, TransactionFunc) error
	Mandatory(context.Context, TransactionFunc) error
	RequiresNew(context.Context, TransactionFunc) error
	NotSupported(context.Context, TransactionFunc) error
	Never(context.Context, TransactionFunc) error
	Nested(context.Context, TransactionFunc) error
}

transaction manager interface

func NewContextTransactionManager

func NewContextTransactionManager(engine EngineInterface) TransactionManager

func NewRoutineTransactionManager

func NewRoutineTransactionManager(engine EngineInterface) TransactionManager

NewRoutineTransactionManager

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL