mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 22:11:29 +00:00
Merge pull request #4022 from jianzhiyao/develop
stmt: fix bug of non concurrent security & add double check
This commit is contained in:
commit
e1386c448c
@ -120,17 +120,24 @@ func (d *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
|
|||||||
|
|
||||||
func (d *DB) getStmt(query string) (*sql.Stmt, error) {
|
func (d *DB) getStmt(query string) (*sql.Stmt, error) {
|
||||||
d.RLock()
|
d.RLock()
|
||||||
if stmt, ok := d.stmts[query]; ok {
|
c, ok := d.stmts[query]
|
||||||
d.RUnlock()
|
d.RUnlock()
|
||||||
return stmt, nil
|
if ok {
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Lock()
|
||||||
|
c, ok = d.stmts[query]
|
||||||
|
if ok {
|
||||||
|
d.Unlock()
|
||||||
|
return c, nil
|
||||||
}
|
}
|
||||||
d.RUnlock()
|
|
||||||
|
|
||||||
stmt, err := d.Prepare(query)
|
stmt, err := d.Prepare(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
d.Unlock()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
d.Lock()
|
|
||||||
d.stmts[query] = stmt
|
d.stmts[query] = stmt
|
||||||
d.Unlock()
|
d.Unlock()
|
||||||
return stmt, nil
|
return stmt, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user