Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQLite Thread Safety

SQLite Thread Safety

Scheduled Pinned Locked Moved Database
databasesqlitequestion
5 Posts 3 Posters 28 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    The SQLite documentation says that SQLite is thread safe for reading but not for writing. Specifically, they say you can have multiple threads reading from the DB, but only one thread should write at one time. Do you think this means that I should also avoid two threads accessing the DB at one time if one of the threads is reading and the other is writing? IOW, does reading need to be serialized as well?

    The difficult we do right away... ...the impossible takes slightly longer.

    L J 2 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      The SQLite documentation says that SQLite is thread safe for reading but not for writing. Specifically, they say you can have multiple threads reading from the DB, but only one thread should write at one time. Do you think this means that I should also avoid two threads accessing the DB at one time if one of the threads is reading and the other is writing? IOW, does reading need to be serialized as well?

      The difficult we do right away... ...the impossible takes slightly longer.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It's "rereads" that are (usually) the problem. If one is writing, and another reading, one for one makes little difference; if the record aren't related. On the other hand, if you're not writing "complete" records at one time, then they have to be "locked". For "rereads" you have to check for "dirty" (e.g. timestamp changes). "Screen data has been updated". With related records, you may need need to lock a table or the entire DB (for a very short time). All calls should be async to avoid UI waits.

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      Richard Andrew x64R 1 Reply Last reply
      0
      • L Lost User

        It's "rereads" that are (usually) the problem. If one is writing, and another reading, one for one makes little difference; if the record aren't related. On the other hand, if you're not writing "complete" records at one time, then they have to be "locked". For "rereads" you have to check for "dirty" (e.g. timestamp changes). "Screen data has been updated". With related records, you may need need to lock a table or the entire DB (for a very short time). All calls should be async to avoid UI waits.

        "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Well that seems related to the issue that I'm encountering. Occasionally I'll receive a "Access denied - Database locked" error message, and I can't understand it because all of my writes are synchronized around a single mutex. So I thought maybe I can't read either when a thread is writing to the DB. It's an INSERT statement that's receiving the Database locked message, so the only other thing I can think of is that there's something wrong with my threading code and it's actually attempting two writes at once.

        The difficult we do right away... ...the impossible takes slightly longer.

        L 1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          The SQLite documentation says that SQLite is thread safe for reading but not for writing. Specifically, they say you can have multiple threads reading from the DB, but only one thread should write at one time. Do you think this means that I should also avoid two threads accessing the DB at one time if one of the threads is reading and the other is writing? IOW, does reading need to be serialized as well?

          The difficult we do right away... ...the impossible takes slightly longer.

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #4

          Richard Andrew x64 wrote:

          Do you think this means that I should also avoid two threads accessing the DB at one time if one of the threads is reading and the other is writing?

          Yes. SQLite CVSTrac[^]

          Wrong is evil and must be defeated. - Jeff Ello

          1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            Well that seems related to the issue that I'm encountering. Occasionally I'll receive a "Access denied - Database locked" error message, and I can't understand it because all of my writes are synchronized around a single mutex. So I thought maybe I can't read either when a thread is writing to the DB. It's an INSERT statement that's receiving the Database locked message, so the only other thing I can think of is that there's something wrong with my threading code and it's actually attempting two writes at once.

            The difficult we do right away... ...the impossible takes slightly longer.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            An insert implies an index update; or a "split" somewhere. A simple append might be different. In effect, the "internal" state becomes dirty. Because it is "lite".

            "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups