...

Source file src/github.com/letsencrypt/boulder/db/transaction.go

Documentation: github.com/letsencrypt/boulder/db

     1  package db
     2  
     3  import "context"
     4  
     5  // txFunc represents a function that does work in the context of a transaction.
     6  type txFunc func(tx Executor) (interface{}, error)
     7  
     8  // WithTransaction runs the given function in a transaction, rolling back if it
     9  // returns an error and committing if not. The provided context is also attached
    10  // to the transaction. WithTransaction also passes through a value returned by
    11  // `f`, if there is no error.
    12  func WithTransaction(ctx context.Context, dbMap DatabaseMap, f txFunc) (interface{}, error) {
    13  	tx, err := dbMap.BeginTx(ctx)
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  	result, err := f(tx)
    18  	if err != nil {
    19  		return nil, rollback(tx, err)
    20  	}
    21  	err = tx.Commit()
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	return result, nil
    26  }
    27  

View as plain text