go-sqlite3
go-sqlite3 copied to clipboard
mutex_mode
is the default mutex_mode is full_mutex, which means sqlite will run in Serialized mode ?
#include <stdio.h>
#include <sqlite3.h>
int main(void) {
int rc;
// Initialize SQLite (optional for modern builds)
rc = sqlite3_initialize();
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot initialize SQLite: %s\n", sqlite3_errstr(rc));
return 1;
}
// Get default threading mode
int thread_mode = sqlite3_threadsafe();
if (thread_mode == 0) {
printf("SQLite is not threadsafe (SQLITE_THREADSAFE=0)\n");
} else {
printf("SQLite is threadsafe (SQLITE_THREADSAFE=%d)\n", thread_mode);
}
// Get current threading mode
int mode = sqlite3_config(SQLITE_CONFIG_GETMUTEX, NULL);
// Note: Direct retrieval of mutex mode isn't exposed directly, so use serialized check instead:
rc = sqlite3_config(SQLITE_CONFIG_SERIALIZED);
if (rc == SQLITE_OK) {
printf("SQLite is running in Serialized mode (full_mutex)\n");
} else {
printf("SQLite is running in a lower thread safety mode\n");
}
// Example: open a DB and check serialized behavior
sqlite3 *db;
rc = sqlite3_open("test.db", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Failed to open DB: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
// Verify mutex mode (for this connection)
sqlite3_mutex *m = sqlite3_db_mutex(db);
if (m) {
printf("Database connection has a valid mutex (full_mutex mode active)\n");
} else {
printf("Database connection has no mutex (single-thread mode)\n");
}
// Example query
sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT);", NULL, NULL, NULL);
sqlite3_close(db);
sqlite3_shutdown();
return 0;
}