GORM Playground Link
https://github.com/go-gorm/playground/pull/1
Description
I'm using Postgres db with GORM and I tried hardly to setup Replica using the following code ` dbMaster, err := gorm.Open(postgres.Open(dsnMaster), &gorm.Config{})
// Configure postgres database to use master-slave and to use connection pooling
dbMaster.Use(dbresolver.Register(dbresolver.Config{
Replicas: []gorm.Dialector{
postgres.Open(dsnSlave),
},
Policy: dbresolver.RandomPolicy{},
}).SetConnMaxIdleTime(time.Hour).
SetConnMaxLifetime(24 * time.Hour).
SetMaxIdleConns(100).
SetMaxOpenConns(200))`
I configured my Postgres database to work on Master-Slave configuration. It's configured successfully and I verified that by using DBeaver where any SQL statement has been executed on master it will be replicated into slave.
The issue is when I use Go with Gorm and try to execute two statements Create and Select, both statements are done on master.
I can confirm this by monitoring both instances of database from the command line where I can see this into master
2022-01-20 01:20:50.697 EET [66242] LOG: execute lrupsc_22_1: select * from comment
2022-01-20 01:20:50.714 EET [66243] LOG: statement: begin
2022-01-20 01:20:50.715 EET [66243] LOG: execute lrupsc_23_0: INSERT INTO "comment" ("created_at","updated_at","deleted_at","abwaab_user_id","interaction_type","parent_interaction_id","body","content_info","is_pinned") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9) RETURNING "id"
2022-01-20 01:20:50.715 EET [66243] DETAIL: parameters: $1 = '2022-01-20 07:20:50.707389+08', $2 = '2022-01-20 07:20:50.714414+08', $3 = '0001-01-01 08:05:43+08:05:43', $4 = '6', $5 = 'reply', $6 = '22', $7 = 'Reply To Hi1', $8 = '', $9 = 'f'
2022-01-20 01:20:50.716 EET [66243] LOG: statement: commit
While slave instance doesn't print anything. Gorm with Postgres isn't working when it comes to Master-Slave mechanism.
This is my code of db
`package storage
import ( "fmt" "gorm.io/driver/postgres" "gorm.io/gorm" "gorm.io/plugin/dbresolver" "os" "scgo/helpers" "time" )
// DBI Global Database Instance variable, to access DB from all functions
var DBI *gorm.DB
func DBConnect() (*gorm.DB, error) { // Get info of master database dbMasterName := os.Getenv("DB_Master_NAME") dbMasterUsername := os.Getenv("DB_Master_USER_NAME") dbMasterURL := os.Getenv("DB_Master_URL") dbMasterPort := os.Getenv("DB_Master_PORT") // Get info of slave database dbSlaveName := os.Getenv("DB_Slave_NAME") dbSlaveUsername := os.Getenv("DB_Slave_USER_NAME") dbSlaveURL := os.Getenv("DB_Slave_URL") dbSlavePort := os.Getenv("DB_Slave_PORT")
dsnMaster := fmt.Sprintf("host=%s user=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
dbMasterURL, dbMasterUsername, dbMasterName, dbMasterPort)
dsnSlave := fmt.Sprintf("host=%s user=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
dbSlaveURL, dbSlaveUsername, dbSlaveName, dbSlavePort)
dbMaster, err := gorm.Open(postgres.Open(dsnMaster), &gorm.Config{})
// Configure postgres database to use master-slave and to use connection pooling
dbMaster.Use(dbresolver.Register(dbresolver.Config{
Replicas: []gorm.Dialector{
postgres.Open(dsnSlave),
},
Policy: dbresolver.RandomPolicy{},
}).SetConnMaxIdleTime(time.Hour).
SetConnMaxLifetime(24 * time.Hour).
SetMaxIdleConns(100).
SetMaxOpenConns(200))
if nil != err {
helpers.Logger.Error("Database is not reachable")
panic("Database is not reachable")
}
helpers.Logger.Debug("Database connection is acquired")
DBI = dbMaster
return dbMaster, nil
} `
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨