开发者

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

开发者 https://www.devze.com 2023-02-28 18:34 出处:网络
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.

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 开发者_Python百科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.

0

精彩评论

暂无评论...
验证码 换一张
取 消