I have two statements in one process

statement 1                        statement 2
-----------                        -----------
prepare("select * from foo")
step() == SQLITE_ROW
                                   prepare("insert into foo values(1)"
                                   step() == SQLITE_ERROR

I would expect for sqlite3_busy_timeout(db, 30000); to make the step()
on statement 2 wait 30 seconds before returning, but it returns
immediately.  Is this the correct behavior?  Also, I would expect for
the return code of the sqlite3_step() on statement 2 to be
SQLITE_LOCKED, not SQLITE_ERROR.

Test case attached.  Tested on 3.0.5, 3.0.6.

Cheers,

Matt

#include <assert.h>
#include <sqlite3.h>
#include <stdio.h>
#include <sys/types.h>
#include <time.h>

int main(void) {
    sqlite3 *pDb;
    sqlite3_stmt *pStmt1, *pStmt2;
    const char *zLeftover;
    int rc, i;
    time_t t1, t2;

    rc = sqlite3_open(":memory:", &pDb);
    assert(rc == SQLITE_OK);

    sqlite3_busy_timeout(pDb, 30000);

    rc = sqlite3_prepare(pDb, "create table foo(bar)", -1, &pStmt1,
                         &zLeftover);
    assert(rc == SQLITE_OK);
    rc = sqlite3_step(pStmt1);
    assert(rc == SQLITE_DONE);
    rc = sqlite3_finalize(pStmt1);
    assert(rc == SQLITE_OK);

    rc = sqlite3_prepare(pDb, "insert into foo values(?)", -1, &pStmt1,
                         &zLeftover);
    assert(rc == SQLITE_OK);
    for (i=0; i<2; i++) {
        rc = sqlite3_bind_int(pStmt1, 1, i);
        assert(rc == SQLITE_OK);
        rc = sqlite3_step(pStmt1);
        assert(rc == SQLITE_DONE);
        rc = sqlite3_reset(pStmt1);
        assert(rc == SQLITE_OK);
    }

    rc = sqlite3_prepare(pDb, "select * from foo", -1, &pStmt1, &zLeftover);
    assert(rc == SQLITE_OK);
    rc = sqlite3_step(pStmt1);
    assert(rc == SQLITE_ROW);
    
    rc = sqlite3_prepare(pDb, "insert into foo values(1)", -1, &pStmt2,
                         &zLeftover);
    assert(rc == SQLITE_OK);
    t1 = time(NULL);
    rc = sqlite3_step(pStmt2);
    t2 = time(NULL);
    printf("elapsed seconds %ld\n", (int) t2 - t1);
    printf("rc is %d error %s\n", rc, sqlite3_errmsg(pDb));
    assert(rc == SQLITE_DONE);
    rc = sqlite3_finalize(pStmt2);
    assert(rc == SQLITE_OK);

    return 0;
}

Reply via email to