Is there a better way to handle the 'database is locked' in SQLite?
The SQLite is unsupported concurrent and only allows serial to write operation.
Description
I have to add the lock code in the per write operation to prevents the 'database is locked',For example, the following:
var globalLock sync.Mutex
type User struct{
Name string
Age int
}
func (u *User) Create(tx *grom.DB){
globalLock.Lock()
defer globalLock.Unlock()
user := User{Name: "xxx", Age: 18}
tx.Create(&user)
}
type Blog struct{
Title string
}
func (b *Blog) Create(tx *grom.DB){
globalLock.Lock()
defer globalLock.Unlock()
blog := Blog{Title: "xxx"}
tx.Create(&blog)
}
I tried to use Hooks to deal with the lock code but found that it was not satisfactory. when the 'tx.Create(&user)' occurs err or panic, AfterCreate doesn't have execute.
func (u *User) BeforeCreate(tx *grom.DB) error{
globalLock.Lock()
return nil
}
func (u *User) AfterCreate(tx *grom.DB) error{
globalLock.Unlock()
retrun 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 ✨
Comment From: jinzhu
https://www.google.com/search?newwindow=1&sxsrf=ALeKk02_kb7nR7HucO8s_XAwnwhhAmlTAg%3A1604459862919&ei=Vh2iX6DdN5j0rQHk9ImwBg&q=golang+sqlite+database+is+locked&oq=golang+sqlite+database+is+locked&gs_lcp=CgZwc3ktYWIQAzICCAA6BggAEAcQHlDBIli0J2DdKGgAcAF4AIABrAGIAbkIkgEDMC43mAEAoAEBqgEHZ3dzLXdpesABAQ&sclient=psy-ab&ved=0ahUKEwig4L3h9ufsAhUYeisKHWR6AmYQ4dUDCA0&uact=5
Comment From: zicla
You may do this:
phyDb, err := db.DB()
phyDb.SetMaxOpenConns(1)
Comment From: nicola-pesavento
You may do this:
go phyDb, err := db.DB() phyDb.SetMaxOpenConns(1)
More info here: https://github.com/mattn/go-sqlite3/issues/274