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)
| 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. |
| 613 | func (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 |