MCPcopy
hub / github.com/go-gorm/gorm / Connection

Method Connection

finisher_api.go:613–632  ·  view source on GitHub ↗

Connection uses a db connection to execute an arbitrary number of commands in fc. When finished, the connection is returned to the connection pool.

(fc func(tx *DB) error)

Source from the content-addressed store, hash-verified

611// Connection uses a db connection to execute an arbitrary number of commands in fc. When finished, the connection is
612// returned to the connection pool.
613func (db *DB) Connection(fc func(tx *DB) error) (err error) {
614 if db.Error != nil {
615 return db.Error
616 }
617
618 tx := db.getInstance()
619 sqlDB, err := tx.DB()
620 if err != nil {
621 return
622 }
623
624 conn, err := sqlDB.Conn(tx.Statement.Context)
625 if err != nil {
626 return
627 }
628
629 defer conn.Close()
630 tx.Statement.ConnPool = conn
631 return fc(tx)
632}
633
634// Transaction start a transaction as a block, return error will rollback, otherwise to commit. Transaction executes an
635// arbitrary number of commands in fc within a transaction. On success the changes are committed; if an error occurs

Callers 1

TestWithSingleConnectionFunction · 0.80

Calls 3

getInstanceMethod · 0.95
DBMethod · 0.80
CloseMethod · 0.65

Tested by 1

TestWithSingleConnectionFunction · 0.64