简体   繁体   English

一起使用 SQLiteConnection 和 SQLiteAsyncConnection

[英]Use SQLiteConnection and SQLiteAsyncConnection together

Given给定的

I use SQLite-Net NuGet package.我使用SQLite-Net NuGet 包。
I have old synchronous API for database manipulations ( SQLiteConnection ).我有用于数据库操作的旧同步 API ( SQLiteConnection )。

What I need我需要的

I want to use new asynchronous API for database manipulations ( SQLiteAsyncConnection )我想使用新的异步 API 进行数据库操作( SQLiteAsyncConnection

Problem问题

Old synchronous API is big enough that I can't do quick migration to async API, so I need to use both synchronous and asynchronous API together using one database file, like this:旧的同步 API 足够大,我无法快速迁移到异步 API,因此我需要使用一个数据库文件同时使用同步和异步 API,如下所示:

var database = new SQLiteConnection(connectionString); // Synchronous API
var databaseAsync = new SQLiteAsyncConnection(connectionString); // Asynchronous API

If i try to use two connections on one database file like above, I getting out of sync problems, such as creating table using synchronous connection not makes table available through asynchronous connection.如果我尝试像上面一样在一个数据库文件上使用两个连接,则会出现不同步的问题,例如使用同步连接创建表不能通过异步连接使表可用。 I assume there is some inner cache inside connection or something...我假设连接内部有一些内部缓存或其他东西......

Question

How can I use one database file and two (sync and async) connections together properly?如何正确使用一个数据库文件和两个(同步和异步)连接?
And.. can I?还有..可以吗?

Additional info附加信息

  • Synchronous and asynchronous API will be fully covered by locks.同步和异步 API 将被锁完全覆盖。 So I expect no conflicts with using both API together.所以我希望同时使用这两个 API 不会发生冲突。

Multiple SQLite processes can have access to the database simultanously.多个 SQLite 进程可以同时访问数据库。

No need to worry about write access as whenever you are writing using one of your connections, the object will lock the database for a short time.无需担心写入访问,因为每当您使用其中一个连接进行写入时,该对象都会将数据库锁定一小段时间。

Your "out of sync" problem are probably coming from the Write Ahead Logging option being desactivated by default.您的“不同步”问题可能来自默认情况下停用的预写日志选项。

To activate it, just append the following segment to your connection string :要激活它,只需将以下部分附加到您的连接字符串:

;PRAGMA journal_mode=WAL;

This should allow concurrent read and write access while you slowly move to the new asynchronous API.当您慢慢转向新的异步 API 时,这应该允许并发读写访问。

On top of that, please also enable the Shared Cache option to share the same data cache between your instances.最重要的是,还请启用共享缓存选项以在您的实例之间共享相同的数据缓存。 Please activate it using the following command :请使用以下命令激活它:

PRAGMA cache=shared

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

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