Skip to content

Commit

Permalink
trying to fix 'database is locked' issue according to the mattn/go-sq…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Aug 30, 2023
1 parent dd64cf3 commit b5e3aff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 9 additions & 3 deletions cmd/census3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ func main() {
flag.Parse()
log.Init(*logLevel, "stdout", nil)

db, q, err := db.Init(*dataDir)
database, q, err := db.Init(*dataDir)
if err != nil {
log.Fatal(err)
}

// Start the holder scanner
hc, err := service.NewHoldersScanner(db, q, *url)
hc, err := service.NewHoldersScanner(database, q, *url)
if err != nil {
log.Fatal(err)
}

// Start the API
err = api.Init(db, q, api.Census3APIConf{
err = api.Init(database, q, api.Census3APIConf{
Hostname: "0.0.0.0",
Port: *port,
DataDir: *dataDir,
Expand All @@ -60,6 +60,12 @@ func main() {
log.Warnf("received SIGTERM, exiting at %s", time.Now().Format(time.RFC850))
cancel()
log.Infof("waiting for routines to end gracefully...")
// closing database
go func() {
if err := database.Close(); err != nil {
log.Fatal(err)
}
}()
time.Sleep(5 * time.Second)
os.Exit(0)
}
6 changes: 5 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ func Init(dataDir string) (*sql.DB, *queries.Queries, error) {
}
}
// open database file
database, err := sql.Open("sqlite3", dbFile)
fileURI := fmt.Sprintf("file:%s?cache=shared", dbFile)
database, err := sql.Open("sqlite3", fileURI)
if err != nil {
return nil, nil, fmt.Errorf("error opening database: %w", err)
}
// trying to fix "database is locked" issue according to the official
// mattn/go-sqlite3 docs: https://github.com/mattn/go-sqlite3/#faq
database.SetMaxOpenConns(1)
// get census3 goose migrations and setup for sqlite3
if err := goose.SetDialect("sqlite3"); err != nil {
return nil, nil, fmt.Errorf("error setting up driver for sqlite: %w", err)
Expand Down

0 comments on commit b5e3aff

Please sign in to comment.