How to set isolation level

早过忘川 提交于 2019-12-11 12:40:42

问题


I want to set isolation level to repeatable read. How do I achieve this using gorm orm for postgres.

Example code:

func CreateAnimals(db *gorm.DB) err {
  tx := db.Begin()
  // Note the use of tx as the database handle once you are within a transaction

  if err := tx.Create(&Animal{Name: "Giraffe"}).Error; err != nil {
     tx.Rollback()
     return err
  }

  if err := tx.Create(&Animal{Name: "Lion"}).Error; err != nil {
     tx.Rollback()
     return err
  }

  tx.Commit()
  return nil
}

回答1:


I had exactly the same problem here:

func GetPageAfterUpdate(dp model.DbProvider, id int, update int, page *Page) (err error) {
  tx := dp.DB().Begin()

  err = tx.Exec(`set transaction isolation level repeatable read`).Error
  if err != nil {
    tx.Rollback()
    return
  }

And it is exactly gorm with pg.



来源:https://stackoverflow.com/questions/31844680/how-to-set-isolation-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!