Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a better way to handle the 'database is locked' in SQLite? #3709

Closed
ArronSeven opened this issue Nov 3, 2020 · 4 comments
Closed
Assignees
Labels

Comments

@ArronSeven
Copy link

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
}
@ArronSeven ArronSeven added the type:question general questions label Nov 3, 2020
@github-actions github-actions bot added type:missing reproduction steps missing reproduction steps and removed type:question general questions labels Nov 3, 2020
@github-actions
Copy link

github-actions bot commented Nov 3, 2020

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.ioSearch Before Asking

@jinzhu jinzhu closed this as completed Nov 4, 2020
@zicla
Copy link

zicla commented Mar 18, 2022

You may do this:

		phyDb, err := db.DB()
		phyDb.SetMaxOpenConns(1)

@nicola-pesavento
Copy link

You may do this:

		phyDb, err := db.DB()
		phyDb.SetMaxOpenConns(1)

More info here: mattn/go-sqlite3#274

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants