简体   繁体   English

如何在Java中同时使用SQLite? 哪个是最适合此目的的包装纸?

[英]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. 首先,我知道SQLite可能不是最适合高并发需求的DBMS,但使用客户端/服务器DBMS对我来说不是一个选择,至少到现在为止。

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. 据我所知,SQLite目前能够支持许多并发读取,但一次只能更新。 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. 为了解决这个问题,我想我应该排队(并因此序列化)更新操作,并同时从thre DB读取。 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. 但是我很无能,我真的不知道怎么做,因为我一直在使用Zentus JDBC驱动程序,这似乎不足以满足我的目的。

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: 关于SQLite并发性,在他们的网页中说:

(...) it is safe to move a connection handle across threads as long as the connection is not holding any fcntl() locks. (...)只要连接没有持有任何fcntl()锁,就可以安全地跨线程移动连接句柄。 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. 多个进程可以同时执行SELECT。 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. 当SQLite尝试访问由另一个进程锁定的文件时,默认行为是返回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. 这取决于您对数据库设计采用的方法类型,但它基本上归结为通过将每个调用汇集到单个同步(或以其他方式锁定)方法的方式执行Femi建议的操作。

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? synchronized()函数会做你想要的吗? I mean, your multiple threads would block waiting for DB access, but if you're reasonably careful that should do what you want. 我的意思是,你的多个线程会阻止等待数据库访问,但是如果你非常小心,应该做你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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