in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

Database locking and threads - exception

Last post 08-24-2010 5:12 AM by sten. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 08-23-2010 2:11 AM

    • sten
    • Top 100 Contributor
    • Joined on 06-14-2010
    • Posts 12

    Database locking and threads - exception

    I have a database that is accessed by two threads. One thread writes to the database and regular intervals while the other thread performs read operations. Each thread has its own connection pointer, and I have synchronized access even further by using lock. In short:

    lock (syncObject)
    {
      using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))
      {
         ...
      }
    }

    SyncObject is shared by the two threads.

    Based on this I think I have followed what should be done when using multiple thread access to the database. However, from time to time I get an SQLite exception telling me that its trying to write an readonly database. Im guessing that the writer thread is trying to write a database that is still opened by the reader thread. The question is how is this possible?

     

  • 08-23-2010 3:01 AM In reply to

    Re: Database locking and threads - exception

    Do you perhaps have TortoiseSVN installed on the same machine?
  • 08-23-2010 6:51 AM In reply to

    Re: Database locking and threads - exception

    Do u have an antivirus?
  • 08-23-2010 7:30 AM In reply to

    • sten
    • Top 100 Contributor
    • Joined on 06-14-2010
    • Posts 12

    Re: Database locking and threads - exception

    navaraf:
    Do you perhaps have TortoiseSVN installed on the same machine?

    Im not using any subversion clients, or subversion for that matter.

  • 08-23-2010 7:35 AM In reply to

    • sten
    • Top 100 Contributor
    • Joined on 06-14-2010
    • Posts 12

    Re: Database locking and threads - exception

    catchertinator:
    Do u have an antivirus?

    Yes, I have antivirus on my machine. Can that be the reason, how, and is that a common issue?

  • 08-23-2010 7:43 AM In reply to

    Re: Database locking and threads - exception

    Instead of repeatedly opening and closing the database connection, why not have each thread open the database once, and then use the syncObject to sync the commands you execute on the connections?

     

  • 08-23-2010 7:52 AM In reply to

    Re: Database locking and threads - exception

    Repeatedly opening and closing the database connection, since it is file-based, will often trigger antivirus programs to repeatedly scan the file.  Frequently, the anti-virus program will briefly lock the file (especially the rollback journal) briefly during the process, and prevent SQLite from reading/writing/creating/locking that journal file.

    You could also try changing the journal mode in your connection string to persist.

  • 08-23-2010 8:02 AM In reply to

    Re: Database locking and threads - exception

    Yes, follow Robert guide and problems will disappear!
  • 08-23-2010 8:35 AM In reply to

    • sten
    • Top 100 Contributor
    • Joined on 06-14-2010
    • Posts 12

    Re: Database locking and threads - exception

    Robert Simpson:
    Instead of repeatedly opening and closing the database connection, why not have each thread open the database once, and then use the syncObject to sync the commands you execute on the connections?

    From what I've read and understood so far the SQLiteConnection class works as a synchronization against the database. So to follow your guideance on this issue, I open the database once and then use the SQLiteConnection to synchronize. However due to the current design I think I will synchronize using lock as im doing now, and have the threads share the connection pointer and close the connection on application termination. There may be more threads that is going to access the database in the future.

    So in essence:

    1. Open the database once upon application startup, and share the connection pointer between threads.
    2. Use c# synchronization mechanism to synchronize access to the connection pointer (database), and perform database operations.
    3. When application terminates/exits, close the connection.

     

    Using this I should achieve the same, if I understood the pinned "Thread safety" thread. Correct me if im wrong, or its a bad idea. Again the main reason I want to do it like this is due to the current design. Let me know if I should look into a redesign to support multiple connection pointers.

  • 08-24-2010 5:12 AM In reply to

    • sten
    • Top 100 Contributor
    • Joined on 06-14-2010
    • Posts 12

    Re: Database locking and threads - exception

    Are there any drawbacks with setting the journal mode to persist?

Page 1 of 1 (10 items)
Powered by Community Server (Commercial Edition), by Telligent Systems