upper / db

Data Access Layer (DAL) for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features.
https://upper.io/
MIT License
3.53k stars 234 forks source link

Sqlite, databse is locked #636

Closed forrackun closed 2 years ago

forrackun commented 3 years ago

hello, i am having database is locked issue. i can reproduce in below minimal example. as far as i know a constrain violation should not lock for subsequent operations can you help me :) ? thank you

// example.go

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/upper/db/v4"
    "github.com/upper/db/v4/adapter/sqlite"
)

var settings = sqlite.ConnectionURL{
    Database: `example.db`,
}

func main() {
    defer os.Remove(`example.db`)

    sess, err := sqlite.Open(settings)
    if err != nil {
        log.Fatalf("db.Open(): %q\n", err)
    }

    _, err = sess.SQL().Exec(`DROP TABLE IF EXISTS "example1";

    create table example1(
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        slug text not null,
        subtype text not null,
        description text not null,
        group_id text not null,
        unique( slug, group_id)
    );`)

    if err != nil {
        log.Fatal(err)
    }

    //sess.SetMaxOpenConns(1)

    defer sess.Close()

    for range []int{1, 2, 3} {
        Insert(sess)
    }
}

func Insert(sess db.Session) {
    tbl := sess.Collection("example1")

    _, err := tbl.Insert(db.Cond{
        "slug":        "aa",
        "group_id":    "g1",
        "description": "asas",
        "subtype":     "asa",
    })
    fmt.Println(err)
}
<nil>
2021/08/27 11:00:55 upper/db: log_level=WARNING file=/home/madman/go/pkg/mod/github.com/upper/db/v4@v4.2.1/internal/sqladapter/session.go:646
        Session ID:     00001
        Query:          INSERT INTO "example1" ("description", "group_id", "slug", "subtype") VALUES (?, ?, ?, ?)
        Arguments:      []interface {}{"asas", "g1", "aa", "asa"}
        Error:          UNIQUE constraint failed: example1.slug, example1.group_id
        Time taken:     0.00055s
        Context:        context.Background

UNIQUE constraint failed: example1.slug, example1.group_id
2021/08/27 11:01:05 upper/db: log_level=WARNING file=/home/madman/go/pkg/mod/github.com/upper/db/v4@v4.2.1/internal/sqladapter/session.go:646
        Session ID:     00001
        Query:          INSERT INTO "example1" ("description", "group_id", "slug", "subtype") VALUES (?, ?, ?, ?)
        Arguments:      []interface {}{"asas", "g1", "aa", "asa"}
        Error:          upper: slow query
        Time taken:     10.01480s
        Context:        context.Background

database is locked
forrackun commented 2 years ago

https://github.com/upper/db/blob/9b9ecf13c7e8e92e4dd300be4ba4dfbfa8db6db1/internal/testsuite/sql_suite.go#L147