InsaneCoder
InsaneCoder

Reputation: 8268

How to Lock and then Unlock sqlite database using c?

I am testing for this question using SQLite 3 database in C language.

On one thread, I am accessing a table 'audio' and on the other thread I am renaming that table to dummy and back to audio. But I get following errors after sometime

  1. database is locked from one thread

  2. cannot start transaction within another transaction from another thread.

I read somewhere that using transaction locks a database and unlocks it after commit. But that's not happening here. Can anyone suggest the mistake or proper solution.

Here is my code :

#include <stdio.h>
#include <pthread.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <unistd.h>

static int callback1(void *data, int argc, char **argv, char **azColName){
   int i;
   fprintf(stderr, "%s: ", (const char*)data);

      printf("%s = %s\n", azColName[0], argv[0] ? argv[0] : "NULL");

   printf("\n");
   return 0;
}

void *dbOps1 (void * name)
{
    sqlite3 *db;
   char *zErrMsg = 0;
   int rc;
   char *sql;
   const char* data = "";

   /* Open database */
   rc = sqlite3_open("myDB.db", &db);
   if( rc ){
      printf("Can't open database: %s\n", sqlite3_errmsg(db));
      exit(0);
   }else{
      printf("Opened database successfully\n");
   }

   /* Create SQL statement */
   sql = "SELECT * from audio";

   /* Execute SQL statement */
   for(;;)
   {
    rc = sqlite3_exec(db, sql, callback1, (void*)data, &zErrMsg);
    if( rc != SQLITE_OK ){
       printf("SQL error: %s\n", zErrMsg);
       sqlite3_free(zErrMsg);
    }else{
       // printf("Operation done successfully\n");
    }
   }
   sqlite3_close(db);
   return 0;
}

static int callback2(void *data, int argc, char **argv, char **azColName){
   int i;
   fprintf(stderr, "%s: ", (const char*)data);

      printf("%s = %s\n", azColName[1], argv[1] ? argv[1] : "NULL");

   printf("\n");
   return 0;
}

void *dbOps2 (void * name)
{
    sqlite3 *db;
   char *zErrMsg = 0;
   int rc;
   char *sql;
   const char* data = "";

   /* Open database */
   rc = sqlite3_open("myDB.db", &db);
   if( rc ){
      printf("Can't open database: %s\n", sqlite3_errmsg(db));
      exit(0);
   }else{
      printf("Opened database successfully\n");
   }

   /* Create SQL statement */
   sql = "Begin transaction;alter table audio rename to dummy;alter table dummy rename to audio;commit transaction;";

   /* Execute SQL statement */
   for(;;)
   {
    rc = sqlite3_exec(db, sql, callback2, (void*)data, &zErrMsg);
    if( rc != SQLITE_OK ){
       printf("SQL error: %s\n", zErrMsg);
       sqlite3_free(zErrMsg);
    }else{
       // printf("Operation done successfully\n");
    }
   }
   sqlite3_close(db);
   return 0;
}

int main()
{
    pthread_t thread1,thread2;
    char * message1="Thread 1";
    char * message2="Thread 2";

    int iret1=pthread_create(&thread1,NULL,dbOps1,(void *)message1);
    int iret2=pthread_create(&thread2,NULL,dbOps2,(void *)message2);

    pthread_join(thread1,NULL);
    pthread_join(thread2,NULL);

    printf("Thread 1 return code :%d\n",iret1);
    printf("Thread 2 return code :%d\n",iret2);
    return 0;
}

Upvotes: 0

Views: 1394

Answers (1)

CL.
CL.

Reputation: 180060

You get the "database is locked" error because you did not set a busy timeout that is long enough. (The default, 0, certainly isn't.)

The "cannot start transaction within another transaction" is just a consequence of not committing or rolling back the first transaction before starting the second one. (sqlite3_exec stops at the first error.)

Upvotes: 1

Related Questions