From c1a93554a34d65a50b02a948f6a6bfd6e35fa17f Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Sun, 31 Mar 2024 13:28:54 +0900 Subject: [PATCH] set finalizer for adapter (#5) --- adapter.go | 7 +++++++ 1 file changed, 7 insertions(+) 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 }