Your Question
I have a service that is copying data from one table to another in mysql using GORM. The Query will take about 10 mins to finish running and one of the requirements is to cancel the running query. After the context cancel or timeout, I see the result.Error will show "context canceled"/"context timeout" and this function will return. However, on the DB side, the query session/process remains open for however long the actual insert takes. I am thinking that GORM isn't terminating a session/process properly during a session timeout/cancel. Is there a way to explicitly terminate a connection on the DB side on context cancel/timeout?
eventContext, cancel := context.WithCancel(ctx) // same issue with eventContext, cancel := context.WithTimeout(ctx, 600*time.Second)
tx := db.WithContext(eventContext).Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
if err := tx.Error; err != nil {
return err
}
result := tx.Exec("'INSERT INTO `Table1` field1 field2 SELECT field1 field2 FROM table 2')
if err := result.Error; err != nil {
tx.Rollback()
return err
}
if err := tx.Commit().Error; err != nil {
return err
}
return nil
The document you expected this should be explained
https://gorm.io/docs/context.html
Expected answer
Instruction on how to end db query session with gorm. Right now the only way to terminate the long running query is to run KILL SPID on db level.
Comment From: github-actions[bot]
This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days