Setup DB's ConnPool in PrepareStmt mode, fix #3362

This commit is contained in:
Jinzhu 2020-09-03 12:54:26 +08:00
parent ff3880292d
commit 98e15e0b95
2 changed files with 6 additions and 0 deletions

View File

@ -176,6 +176,8 @@ func (db *DB) Session(config *Session) *DB {
Mux: preparedStmt.Mux, Mux: preparedStmt.Mux,
Stmts: preparedStmt.Stmts, Stmts: preparedStmt.Stmts,
} }
txConfig.ConnPool = tx.Statement.ConnPool
txConfig.PrepareStmt = true
} }
} }

View File

@ -12,6 +12,10 @@ import (
func TestPreparedStmt(t *testing.T) { func TestPreparedStmt(t *testing.T) {
tx := DB.Session(&gorm.Session{PrepareStmt: true}) tx := DB.Session(&gorm.Session{PrepareStmt: true})
if _, ok := tx.ConnPool.(*gorm.PreparedStmtDB); !ok {
t.Fatalf("should assign PreparedStatement Manager back to database when using PrepareStmt mode")
}
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel() defer cancel()
txCtx := tx.WithContext(ctx) txCtx := tx.WithContext(ctx)