Close the database
()
| 193 | |
| 194 | // Close the database |
| 195 | func (this *DB) Close() error { |
| 196 | // check database status |
| 197 | this.statusLocker.Lock() |
| 198 | if this.isClosing { |
| 199 | this.statusLocker.Unlock() |
| 200 | return nil |
| 201 | } |
| 202 | this.isClosing = true |
| 203 | this.statusLocker.Unlock() |
| 204 | |
| 205 | // waiting for updating operations to finish |
| 206 | var maxLoops = 5_000 |
| 207 | for { |
| 208 | this.statusLocker.Lock() |
| 209 | var countUpdating = this.countUpdating |
| 210 | this.statusLocker.Unlock() |
| 211 | if countUpdating <= 0 { |
| 212 | break |
| 213 | } |
| 214 | time.Sleep(1 * time.Millisecond) |
| 215 | |
| 216 | maxLoops-- |
| 217 | if maxLoops <= 0 { |
| 218 | break |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | for _, batch := range this.batches { |
| 223 | batch.close() |
| 224 | } |
| 225 | |
| 226 | events.Remove(fmt.Sprintf("db_%p", this)) |
| 227 | |
| 228 | defer func() { |
| 229 | if this.locker != nil { |
| 230 | _ = this.locker.Release() |
| 231 | } |
| 232 | }() |
| 233 | |
| 234 | // print log |
| 235 | /**if len(this.dsn) > 0 { |
| 236 | u, _ := url.Parse(this.dsn) |
| 237 | if u != nil && len(u.Path) > 0 { |
| 238 | remotelogs.Debug("DB", "close '"+u.Path) |
| 239 | } |
| 240 | }**/ |
| 241 | |
| 242 | return this.rawDB.Close() |
| 243 | } |
| 244 | |
| 245 | func (this *DB) BeginUpdating() bool { |
| 246 | this.statusLocker.Lock() |