# Question: more resilient to panic while db is closed but there may be some ongoing write transactions

**URL:** <https://discuss.dgraph.io/t/question-more-resilient-to-panic-while-db-is-closed-but-there-may-be-some-ongoing-write-transactions/11680>\
**Category:** Badger\
**Tags:** kind:question\
**Created:** [December 1, 2020, 1:13am UTC](https://discuss.dgraph.io/t/question-more-resilient-to-panic-while-db-is-closed-but-there-may-be-some-ongoing-write-transactions/11680 "2020-12-01T01:13:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hu13](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/hu13/32/5526_2.png) [@hu13](https://discuss.dgraph.io/u/hu13)\
**Post date:** [December 1, 2020, 1:13am UTC](https://discuss.dgraph.io/t/question-more-resilient-to-panic-while-db-is-closed-but-there-may-be-some-ongoing-write-transactions/11680/1 "2020-12-01T01:13:57Z")

</div>

### What version of Go are you using (`go version`)?

```
$ go version
go version go1.13.15 darwin/amd64
```

### What operating system are you using?

macOS big sur 11.0.1

### What version of Badger are you using?

1.6.2

### Does this issue reproduce with the latest master?

Not sure

### What Badger options were set?

default option.

Hi there, I’ve been trying badgerDB in one of my projects.  
Basically I have some object A that contains a db reference and will create write transactions, etc.

However, at any time, there could be a user who will trigger an event to get rid of object A. This in turns should trigger a db’s close and terminates any on going db work.  
However, some of the db work is done in the background and won’t be tracked.

If such background work tries to use a write transaction while the db is already closed, we will get a panic in the application.  
Therefore, I am wondering if there is a way to say detect that `connection is closed`, don’t panic in those background work, but just clean up and exit gracefully?

As a comparison, If I use sqlite3 as my db store instead, I will just get connection closed error in the background work logic and can exit cleanly.

Thanks

---

<div class="post-metadata">

**Author:** ![ibrahim](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/ibrahim/32/6387_2.png) [@ibrahim](https://discuss.dgraph.io/u/ibrahim)\
**Post date:** [December 7, 2020, 10:35am UTC](https://discuss.dgraph.io/t/question-more-resilient-to-panic-while-db-is-closed-but-there-may-be-some-ongoing-write-transactions/11680/3 "2020-12-07T10:35:40Z")

</div>

Hey @hu13, you can use the `db.IsClosed()` API in your background routines.

> <https://github.com/dgraph-io/badger/blob/0f77c00a34d49d7e4b2e417c5f0899c6341712bb/db.go#L476>

There is still a chance that the transaction could panic and you should use [Defer, Panic, and Recover - The Go Blog](https://blog.golang.org/defer-panic-and-recover) to recover from it. It’s hard to ensure that the transaction remains valid when the DB is being closed.
