Skip to content

Commit 9eb6c60

Browse files
authored
fix: fix context propagation in UseDB method (#1278)
* fix: context propagation in useDB method Signed-off-by: hlts2 <[email protected]> * fix: add nil check for statement Signed-off-by: hlts2 <[email protected]> --------- Signed-off-by: hlts2 <[email protected]>
1 parent f0be7fa commit 9eb6c60

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

do.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ var (
5050

5151
// UseDB specify a db connection(*gorm.DB)
5252
func (d *DO) UseDB(db *gorm.DB, opts ...DOOption) {
53-
db = db.Session(&gorm.Session{Context: context.Background()})
53+
ctx := context.Background()
54+
if db.Statement != nil && db.Statement.Context != nil {
55+
ctx = db.Statement.Context
56+
}
57+
db = db.Session(&gorm.Session{Context: ctx})
5458
d.db = db
5559
config := &DOConfig{}
5660
for _, opt := range opts {

0 commit comments

Comments
 (0)