Results 1 to 10 of 10

Thread: QSqlite, multiple connections to in memory database.

  1. #1
    Join Date
    Mar 2010
    Location
    Gdynia, Poland
    Posts
    12
    Thanked 3 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question QSqlite, multiple connections to in memory database.

    Hi.

    I'd like to ask you people is there any possibility to create more than one concurrent connection to the SQLite database which is located in memory.
    I want to read the database from several threads (different tables). There is no problem to do this on file based database, but I’ve some problems wit memory based one.

    Could somebody can give me a hint how to do this.

    Regards

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlite, multiple connections to in memory database.

    Can you describe the problem you come across?

  3. #3
    Join Date
    Mar 2010
    Location
    Gdynia, Poland
    Posts
    12
    Thanked 3 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite, multiple connections to in memory database.

    Hi.

    I'll try:

    We've a connection to the daytabase in memory:

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "connection1");
    2. db.setDatabaseName(":memory:");
    3. if (!db.open()) {
    4. return false;
    5. }
    To copy to clipboard, switch view to plain text mode 

    The question is how to create a secod connection to the same database which is named ":memory:".

    In the next thread we're doing :
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "connection2");
    2. db.setDatabaseName(":memory:");
    3. if (!db.open()) {
    4. return false;
    5. }
    To copy to clipboard, switch view to plain text mode 

    The connection is opened but we're receiving the new instance of empty database.

    If the database is working on file i.e.
    Qt Code:
    1. in one thread
    2. db.setDatabaseName("myFile.db");
    3.  
    4. on second
    5. db.setDatabaseName("myFile.db");
    6. the problem disapears and everything seams to be OK.
    To copy to clipboard, switch view to plain text mode 
    we've two paralel connections.

    The question is how to acheive the same result in memory database. Is it possible?

  4. #4
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlite, multiple connections to in memory database.

    You don't need to open a second connection. The sole connection can be used from any thread.

  5. #5
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlite, multiple connections to in memory database.

    Why do you need two transactions in parallel? In general only one
    connection can have a transaction open on a database at any time.
    Even with two connections, you can't have two active transactions. The second will stall waiting
    for the first to complete.

  6. #6
    Join Date
    Mar 2010
    Location
    Gdynia, Poland
    Posts
    12
    Thanked 3 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite, multiple connections to in memory database.

    Hi in fact we've tested connection fo file based database with the SQLite driver. File consists of several tables. One thread was writting to tabe A and the second one was reading from table B. It was working quite OK and in paralel.
    Two transactions were executed at the same moment.
    Now we're looking to have the same behaviour with the memory based database.

  7. #7
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlite, multiple connections to in memory database.

    As far as I know , the scenario of having different connections in each thread to the same physical file
    is not applicable to in-memory database because you can not open several connections to the same in-memory
    database.

  8. #8
    Join Date
    Mar 2010
    Location
    Gdynia, Poland
    Posts
    12
    Thanked 3 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite, multiple connections to in memory database.

    We've thought that i.e. memory mapped file in shared memory segment could be a solution, but the problem is thet the file could be expanded during the write transactions.
    And in Qt4 mechanism for mamory shared files is not existing anymore.

  9. #9
    Join Date
    Mar 2010
    Location
    Gdynia, Poland
    Posts
    12
    Thanked 3 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Cool Re: QSqlite, multiple connections to in memory database.

    Hi everybdy.

    We did it.
    We've made our own driver to SQLite. In fact we've used sqlite3_open_v2 methd instead original sqlite3_open_v16 that is implemented in Qt default driver with flag set to NO MUTEXES. At run time we've not serialized acces to in memory database.

    If fact the most mportat thing is to spawn only one connection per thread, but it's wrking in paralel.

  10. #10
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlite, multiple connections to in memory database.

    Would like to have a look. Do your mind showing us some code...

Similar Threads

  1. QSQLITE database can't exec queries
    By mcwar in forum Qt Programming
    Replies: 5
    Last Post: 12th January 2010, 23:47
  2. QSqlite database lock + Delegate + QSqlQueryModel
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 13th October 2009, 11:52
  3. Multiple connections with QTcpSockets
    By DrDonut in forum Qt Programming
    Replies: 1
    Last Post: 11th September 2009, 10:58
  4. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  5. Qt on X11 with multiple display connections?
    By grenoble in forum Qt Programming
    Replies: 1
    Last Post: 25th February 2008, 12:44

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.