Your Question
Similar to this: https://github.com/go-gorm/gorm/issues/5406
but I am not sure I want to use heavyweight connections.
I want to set something like app.current_tenant=1 and change this based on stuff inside my code whenever I connect to the DB. I am doing this by calling
db.Session(&gorm.Session{NewDB: true}).Exec(fmt.Sprintf("SET app.current_tenant=%d", tenantId))
every time I want to execute new GORM functions, but sometimes I get errors saying that the current_tenant is not set. This makes me think that gorm is doing something in the backend like re-using or initializing new sessions or transactions without the configuration set.
I am now trying
a.db.Connection(func(tx *gorm.DB) error {
tx.Exec(fmt.Sprintf("SET app.current_tenant=%d", tenantId));
.... custom code here
})
but I was wondering if I could do this at the transaction level, e.g. by using a.db.Transaction instead, and also was wondering why my initial method was not working sometimes.
The document you expected this should be explained
https://gorm.io/docs/session.html
Expected answer
How do I safely and efficiently set a configuration parameter that won't get overwritten or ignored by gorm.
Comment From: li-jin-gou
I think it is possible to use DB.Connection, the underlying database package of Go used a connection pool.