Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite file locking and DropBox

I'm developing an app in Visual C++ that uses an SQLite3 DB for storing data. Usually it sits in the tray most of the time.

I also would like to enable putting my app in a DropBox folder to share it across several PCs. It worked really well up until DropBox has recently updated itself. And now it says that it "can't sync the file in use". The SQLite file is open in my app, but the lock is shared. There are some prepared statements, but all are reset immediately after using step.

Is there any way to enable synchronizing of an open SQLite database file? Thanks!

Here is the simple wrapper that I use just for testing (no error handling), in case this helps:

 class Statement
 {
 private:
  Statement(sqlite3* db, const std::wstring& sql) : db(db)
  {
   sqlite3_prepare16_v2(db, sql.c_str(), sql.length() * sizeof(wchar_t), &stmt, NULL);
  }

 public:
  ~Statement() { sqlite3_finalize(stmt); }

 public:
  void reset() { sqlite3_reset(stmt); }
  int step() { return sqlite3_step(stmt); }
  int getInt(int i) const { return sqlite3_column_int(stmt, i); }

  std::wstring getText(int i) const
  {
   const wchar_t* v = (const wchar_t*)sqlite3_column_text16(stmt, i);
   int sz = sqlite3_column_bytes16(stmt, i) / sizeof(wchar_t);
   return std::wstring(v, v + sz);
  }

 private:
  friend class Database;

  sqlite3* db;
  sqlite3_stmt* stmt;
 };


 class Database
 {
 public:
  Database(const std::wstring& filename = L"")) : db(NULL)
  {
   sqlite3_open16(filename.c_str(), &db);
  }

  ~Database() { sqlite3_close(db); } 

  void exec(const std::wstring& sql)
  {
   auto_ptr<Statement> st(prepare(sql));
   st->step();   
  }

  auto_ptr<Statement> prepare(const std::wstring& sql) const
  {
   return auto_ptr<Statement>(new Statement(db, sql));
  }

 private:
  sqlite3* db;
 };

UPD: Tried commenting out all calls to LockFile and LockFileEx in sqlite3.c - same result.

UPD2: Tried to call sqlite3_close when idle (just as proof of concept) - still same result! Filemon tells that file is still not closed, only unlocked.

UPD3: Autocommit mode is on. The numbers of BEGINs and COMMITs match (class Transaction and RAII take care of that). SQliteManager is able to connect to the DB while my app is running and make modifications to it.

like image 545
Alex Jenter Avatar asked Mar 12 '10 05:03

Alex Jenter


People also ask

Why you should not use SQLite?

High write volumes: SQLite allows only one write operation to take place at any given time, which significantly limits its throughput. If your application requires lots of write operations or multiple concurrent writers, SQLite may not be adequate for your needs.

Does SQLite lock entire database?

But only one process can be making changes to the database at any moment in time, however. A single write to the database however, does lock the database for a short time so nothing can access it at all (not even reading). Details may be found in File Locking And Concurrency In SQLite Version 3.

Is SQLite good for caching?

Many applications use SQLite as a cache of relevant content from an enterprise RDBMS. This reduces latency, since most queries now occur against the local cache and avoid a network round-trip. It also reduces the load on the network and on the central database server.


2 Answers

Checking the result from sqlite3_close(). Perhaps it is not working because you have not finalized all your prepared statements.

like image 60
Doug Currie Avatar answered Oct 17 '22 01:10

Doug Currie


What file system are you using?

Are you sure autocommit is on and/or that you are committing your statements? I remember having an issue with not committing and the lock would stay on.

like image 45
coderintherye Avatar answered Oct 17 '22 01:10

coderintherye