mattn/go-sqlite3

Where is WAL mode

fairking opened this issue · 5 comments

Hi guys.

Reading the doc I have found an example which uses shared cache mode: file:test.db?cache=shared&mode=memory. But referring to the sqlite3 doc it says it is discouraged and WAL mode should be used instead. Could someone explain how to enable WAL mode? Does _journal_mode refers to WAL mode or there is something else.

Ok. I think I have found an answer: https://www.sqlite.org/wal.html (3. Activating And Configuring WAL Mode)

@fairking How to config wal mode by go-sqlite3?

@fairking How to config wal mode by go-sqlite3?

Please see my implementation of db factory. Please note I am using go-rel but the connection string looks the same as it uses go-sqlite3:

package data

import "github.com/go-rel/rel"

type DbContextFactory struct {
	connection    *string
	stale_repo    rel.Repository
	stale_adapter rel.Adapter
}

func NewDbContextFactory(test_id string, readonly bool, pass string) DbContextFactory {

	// https://github.com/mattn/go-sqlite3#connection-string
	var connection string
	if test_id != "" {
		connection = "file:data_" + test_id + ".db?mode=memory&_journal=MEMORY"
	} else {

		encrypted := ""
		if pass != "" {
			encrypted = "&_auth&_auth_user=napos&_auth_pass=" + pass + "&_auth_crypt=sha512"
		}

		if readonly {
			connection = "file:data.db?mode=ro&_journal=WAL" + encrypted
		} else {
			connection = "file:data.db?mode=rwc&_journal=WAL" + encrypted
		}
	}

	db_factory := DbContextFactory{
		connection: &connection,
	}

	if test_id != "" {
		db := NewDbContext(&db_factory)
		db_factory.stale_repo = db.repo
		db_factory.stale_adapter = db.adapter
	}

	return db_factory
}

For more details please refer the doc: https://github.com/mattn/go-sqlite3#connection-string
I hope it helps.

@fairking In WAL mode, one transaction continues to write to the SQLite database, and another transaction will report the error "database is locked" when creating, updating, and inserting data. Is this a WAL mode configuration problem?

@fairking In WAL mode, one transaction continues to write to the SQLite database, and another transaction will report the error "database is locked" when creating, updating, and inserting data. Is this a WAL mode configuration problem?

Could you please try to change _synchronous to NORMAL? I believe by default it is set to FULL. https://github.com/mattn/go-sqlite3#connection-string

See also the docs: https://www.sqlite.org/pragma.html#pragma_synchronous