可以在同一个数据库文件上使用多个并发 read/write 操作但在 Sembast 中使用不同的存储吗?
Is it okay to use multiple concurrent read/write operations on the same database file but with different stores in Sembast?
我的应用程序具有使用 Sembast DB 在本地存储的配置文件和工作数据库(以及其他数据库)。
请看下面的两个例子,哪个是异步进程的更好实践?
示例 1:
final profileDBPath = p.join(appDocumentDir.path, dbDirectory, 'profile.db');
final profileDB = await databaseFactoryIo.openDatabase(profileDBPath);
final workDBPath = p.join(appDocumentDir.path, dbDirectory, 'work.db');
final workDB = await databaseFactoryIo.openDatabase(workDBPath);
final workStore = stringMapStoreFactory.store('work');
final profileStore = stringMapStoreFactory.store('profile');
示例 2:
final dbPath = p.join(appDocumentDir.path, dbDirectory, 'database.db');
final database = await databaseFactoryIo.openDatabase(dbPath);
final workStore = stringMapStoreFactory.store('work');
final profileStore = stringMapStoreFactory.store('profile');
请注意示例 1 为每个 profile
和 work
打开了两个不同的数据库文件。示例 2 对两者使用相同的 database
文件。
请问稳定性哪个好?
为了简化编码,我更喜欢示例 2,但我担心在异步操作中,示例 2 在他们同时写入同一文件时会崩溃。有什么想法吗?
谢谢
Example 2 will crash when they write on the same file at the same time
我不知道这是你的实验还是只是一个假设。 Sembast 数据库支持多个并发的读取器和单个写入器(单进程和单隔离),并且会适当地使用一种互斥锁来保证数据的一致性。并发写入将被序列化,不应触发任何崩溃。如果是这样,那就是您应该填补的错误!
就个人而言,我会选择单个数据库,它允许跨存储事务处理以实现 2 个数据库无法提供的数据一致性。
我的应用程序具有使用 Sembast DB 在本地存储的配置文件和工作数据库(以及其他数据库)。 请看下面的两个例子,哪个是异步进程的更好实践?
示例 1:
final profileDBPath = p.join(appDocumentDir.path, dbDirectory, 'profile.db');
final profileDB = await databaseFactoryIo.openDatabase(profileDBPath);
final workDBPath = p.join(appDocumentDir.path, dbDirectory, 'work.db');
final workDB = await databaseFactoryIo.openDatabase(workDBPath);
final workStore = stringMapStoreFactory.store('work');
final profileStore = stringMapStoreFactory.store('profile');
示例 2:
final dbPath = p.join(appDocumentDir.path, dbDirectory, 'database.db');
final database = await databaseFactoryIo.openDatabase(dbPath);
final workStore = stringMapStoreFactory.store('work');
final profileStore = stringMapStoreFactory.store('profile');
请注意示例 1 为每个 profile
和 work
打开了两个不同的数据库文件。示例 2 对两者使用相同的 database
文件。
请问稳定性哪个好? 为了简化编码,我更喜欢示例 2,但我担心在异步操作中,示例 2 在他们同时写入同一文件时会崩溃。有什么想法吗?
谢谢
Example 2 will crash when they write on the same file at the same time
我不知道这是你的实验还是只是一个假设。 Sembast 数据库支持多个并发的读取器和单个写入器(单进程和单隔离),并且会适当地使用一种互斥锁来保证数据的一致性。并发写入将被序列化,不应触发任何崩溃。如果是这样,那就是您应该填补的错误!
就个人而言,我会选择单个数据库,它允许跨存储事务处理以实现 2 个数据库无法提供的数据一致性。