diff --git a/adapter.go b/adapter.go index 7d5c6d0..05c0283 100644 --- a/adapter.go +++ b/adapter.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "runtime" "github.com/casbin/casbin/v2/model" "github.com/casbin/casbin/v2/persist" @@ -59,6 +60,12 @@ func NewAdapter(driverName, dataSourceName string, opts ...adapterOption) (*bunA return nil, err } + runtime.SetFinalizer(b, func(a *bunAdapter) { + if err := a.db.Close(); err != nil { + panic(err) + } + }) + return b, nil }