Skip to content

Commit

Permalink
增加Master方法,允许用户明确使用主库
Browse files Browse the repository at this point in the history
  • Loading branch information
lun committed Sep 29, 2019
1 parent dc316f8 commit 6310b4b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func (db ctxDB) getDBSQLInNoTxQuery() (dbSQL SQLCommon) {
return
}

//明确表示使用主库:
// 由于上面的getDBSQLInNoTxQuery方法在取不到dbSQLSlave时候会使用主库,
// 所以这里简单起见,把dbSQLSlave置nil,
// 如果没有主库,那么后面执行sql时候会报空指针的错误,符合逻辑
func (db *ctxDB) useMaster() {
db.dbSQLSlave = nil
}

func beginSeg(db ctxDB, query string, args ...interface{}) (seg *xray.Segment) {
if db.ctx == nil {
logrus.Warn("nil context, forget call WithContext?") //只是warn而不是panic,免得不小心没用WithContext导致服务不可用
Expand Down Expand Up @@ -239,6 +247,16 @@ func (s *DB) DBSlave() *sql.DB {
return db
}

//明确表示使用主库:
// 由于从库和主库有几毫秒的延迟,
// 所以写主库,然后立刻读从库这一行时候,可能未读到修改(如果用事务读,就读的是主库,没这个问题),
// 因此增加这个Master方法
func (s *DB) Master() *DB {
clone := s.clone()
clone.db.useMaster()
return clone
}

// CommonDB return the underlying `*sql.DB` or `*sql.Tx` instance, mainly intended to allow coexistence with legacy non-GORM code.
func (s *DB) CommonDB() SQLCommon {
return s.db
Expand Down

0 comments on commit 6310b4b

Please sign in to comment.