简体   繁体   中英

How to use SQLite concurrently in Java? Which is the best wrapper for that purpose?

First of all, I know that SQLite is not probably the most appropriate DBMS for high concurrency needs, but using a client/server DBMS is not an option for me, at least by now.

The application which I'm coding is probably going to generate quite a lot of queries and updates from different threads. They are not going to be really heavy or complicated updates, probably small and fast most of them, but really unpredictably concurrent, as they depend on information arrived from clients on the network. As far as I know, SQLite is currently able to support many concurrent reads, but only an update at a time. When it writes, needs to acquire an exclusive lock, and returns an exception is something else is beeing written.

To solve this problem, I thought I should queue (and thus serialize) the update operations, and read from thre DB concurrently. This should de enough for me. But I'm clueless, I don't really know how to do this, since I have been using the Zentus JDBC driver, which seems to be not enough for what I'm trying to do.

That's my question, how do you think I should do it? Which is thre appropriate wrapper to achieve this?

Thanks in advance! :)

UPDATE:

About SQLite concurrency, in their web-page say:

(...) it is safe to move a connection handle across threads as long as the connection is not holding any fcntl() locks. You can safely assume that no locks are being held if no transaction is pending and all statements have been finalized.

And:

Multiple processes can have the same database open at the same time. Multiple processes can be doing a SELECT at the same time. But only one process can be making changes to the database at any moment in time, however.

(...)

When SQLite tries to access a file that is locked by another process, the default behavior is to return SQLITE_BUSY.

Hope this helps :).

It depends on the type of approach you are taking to your database design, but it basically boils down to doing something along the lines of what Femi suggested, by funneling every call into a single, synchronized (or otherwise locked) method.

public class DatabaseUpdater
{
    private static final Lock lock = new ReentrantLock();

    public static void doUpdate(DatabaseObject db) throws SQLException
    {
        lock.lock();

        try
        {
            db.doWork();
        }
        finally
        {
            lock.unlock();
        }
    }
}

As long as everyone doing updates goes through the above call, then it will synchronize every call.

Would a synchronized() function do what you want? I mean, your multiple threads would block waiting for DB access, but if you're reasonably careful that should do what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM