Skip to content

Commit

Permalink
Fix 修复SQLITE_BUSY
Browse files Browse the repository at this point in the history
  • Loading branch information
paoka1 committed Jan 15, 2024
1 parent e03b5da commit e51ee27
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
1 change: 0 additions & 1 deletion cmd/ssug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func init() {
func Main() {
p := base.ParsePara()
data.Redirect.Init(p.Key, p.TTL)
defer data.Redirect.Close()
base2.RemoveExp()
base2.SetInitLen(p.InitLen)

Expand Down
4 changes: 0 additions & 4 deletions modules/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ func (r *redirect) GetKey() string {
return r.accessKey
}

func (r *redirect) Close() {
r.db.close()
}

// AddMapping 添加新的映射
func (r *redirect) AddMapping(originalURL string, shortURL string) (Mapping, error) {
r.l.Lock()
Expand Down
15 changes: 11 additions & 4 deletions modules/data/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ func (d *database) open() (*sql.DB, error) {
return db, nil
}

func (d *database) close() {
_ = d.db.Close()
}

// 向数据库添加短链映射
func (d *database) addMapping(m Mapping) error {
insSQL := "INSERT INTO %s VALUES (?, ?, ?);"
Expand Down Expand Up @@ -122,9 +118,13 @@ func (d *database) hasOriginalURL(originalURL string) bool {
if rows.Err() != nil {
return false
}
defer func(rows *sql.Rows) {
_ = rows.Close()
}(rows)
if !rows.Next() {
return false
}
_ = rows.Close()
return true
}

Expand All @@ -145,9 +145,13 @@ func (d *database) hasShortURL(shortURL string) bool {
if rows.Err() != nil {
return false
}
defer func(rows *sql.Rows) {
_ = rows.Close()
}(rows)
if !rows.Next() {
return false
}
_ = rows.Close()
return true
}

Expand All @@ -172,6 +176,9 @@ func (d *database) getRemove(time int64) ([]Mapping, error) {
_ = rows.Scan(&m.ShortURL, &m.OriginalURL, &m.ExpirationTime)
ms = append(ms, m)
}
defer func(rows *sql.Rows) {
_ = rows.Close()
}(rows)
return ms, nil
}

Expand Down

0 comments on commit e51ee27

Please sign in to comment.