From cef3de694d9615c574e82dfa0b50fc7ea2816f3e Mon Sep 17 00:00:00 2001 From: jessetang <1430482733@qq.com> Date: Sun, 13 Nov 2022 11:12:09 +0800 Subject: [PATCH] cleanup(prepare_stmt.go): unnecessary map delete (#5849) --- gorm.go | 2 +- prepare_stmt.go | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/gorm.go b/gorm.go index 589fc4ff..89488b75 100644 --- a/gorm.go +++ b/gorm.go @@ -179,7 +179,7 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) { preparedStmt := &PreparedStmtDB{ ConnPool: db.ConnPool, - Stmts: map[string](*Stmt){}, + Stmts: make(map[string]*Stmt), Mux: &sync.RWMutex{}, PreparedSQL: make([]string, 0, 100), } diff --git a/prepare_stmt.go b/prepare_stmt.go index 7591e533..e09fe814 100644 --- a/prepare_stmt.go +++ b/prepare_stmt.go @@ -47,13 +47,12 @@ func (db *PreparedStmtDB) Close() { func (db *PreparedStmtDB) Reset() { db.Mux.Lock() defer db.Mux.Unlock() - for query, stmt := range db.Stmts { - delete(db.Stmts, query) + + for _, stmt := range db.Stmts { go stmt.Close() } - db.PreparedSQL = make([]string, 0, 100) - db.Stmts = map[string](*Stmt){} + db.Stmts = make(map[string]*Stmt) } func (db *PreparedStmtDB) prepare(ctx context.Context, conn ConnPool, isTransaction bool, query string) (Stmt, error) { @@ -93,7 +92,7 @@ func (db *PreparedStmtDB) prepare(ctx context.Context, conn ConnPool, isTransact // Reason why cannot lock conn.PrepareContext // suppose the maxopen is 1, g1 is creating record and g2 is querying record. - // 1. g1 begin tx, g1 is requeued because of waiting for the system call, now `db.ConnPool` db.numOpen == 1. + // 1. g1 begin tx, g1 is requeue because of waiting for the system call, now `db.ConnPool` db.numOpen == 1. // 2. g2 select lock `conn.PrepareContext(ctx, query)`, now db.numOpen == db.maxOpen , wait for release. // 3. g1 tx exec insert, wait for unlock `conn.PrepareContext(ctx, query)` to finish tx and release. stmt, err := conn.PrepareContext(ctx, query)