SQLite.NET PCL 繁忙异常
SQLite.NET PCL Busy Exception
我们正在 Xamarin 应用程序中使用 SQLite.NET PCL。
当通过向多个表中插入数据使数据库承受压力时,我们看到抛出 BUSY 异常。
谁能解释一下 BUSY 和 LOCKED 之间的区别?是什么导致数据库繁忙?
我们的代码使用到使用以下代码创建的数据库的单一连接:
var connectionString = new SQLiteConnectionString(GetDefaultConnectionString(),
_databaseConfiguration.StoreTimeAsTicks);
var connectionWithLock = new SQLiteConnectionWithLock(new SQLitePlatformAndroid(), connectionString);
return new SQLiteAsyncConnection (() => { return connectionWithLock; });
...current operation cannot proceed because the required resources are locked...
我假设您正在使用异步风格的插入并且在不同的线程上因此插入超时等待不同插入的锁来完成。您可以使用同步插入来避免这种情况。我个人在需要时通过创建 FIFO 队列并在专用线程上同步使用该队列来避免这种情况。您还可以通过在 Exception
波动之前重试您的交易 X 次来处理这种情况。
SQLiteBusyException is a special exception that is thrown whenever SQLite returns SQLITE_BUSY or SQLITE_IOERR_BLOCKED error code. These codes mean that the current operation cannot proceed because the required resources are locked.
When a timeout is set via SQLiteConnection.setBusyTimeout(long), SQLite will attempt to get the lock during the specified timeout before returning this error.
所以我们的问题是,虽然我们已经确保在 class 中我们写了它只创建了一个到数据库的连接,但我们没有确保这个 class是一个单身人士,因此我们仍在创建到数据库的多个连接。一旦我们确定它是一个单例,那么繁忙的错误就会停止
我从中得到的是:
锁定意味着您有多个线程试图访问数据库,代码本质上不是线程安全的。
忙碌意味着您有一个线程正在等待另一个线程完成,您的代码是线程安全的,但您在使用数据库时看到争用。
我们正在 Xamarin 应用程序中使用 SQLite.NET PCL。
当通过向多个表中插入数据使数据库承受压力时,我们看到抛出 BUSY 异常。
谁能解释一下 BUSY 和 LOCKED 之间的区别?是什么导致数据库繁忙?
我们的代码使用到使用以下代码创建的数据库的单一连接:
var connectionString = new SQLiteConnectionString(GetDefaultConnectionString(),
_databaseConfiguration.StoreTimeAsTicks);
var connectionWithLock = new SQLiteConnectionWithLock(new SQLitePlatformAndroid(), connectionString);
return new SQLiteAsyncConnection (() => { return connectionWithLock; });
...current operation cannot proceed because the required resources are locked...
我假设您正在使用异步风格的插入并且在不同的线程上因此插入超时等待不同插入的锁来完成。您可以使用同步插入来避免这种情况。我个人在需要时通过创建 FIFO 队列并在专用线程上同步使用该队列来避免这种情况。您还可以通过在 Exception
波动之前重试您的交易 X 次来处理这种情况。
SQLiteBusyException is a special exception that is thrown whenever SQLite returns SQLITE_BUSY or SQLITE_IOERR_BLOCKED error code. These codes mean that the current operation cannot proceed because the required resources are locked.
When a timeout is set via SQLiteConnection.setBusyTimeout(long), SQLite will attempt to get the lock during the specified timeout before returning this error.
所以我们的问题是,虽然我们已经确保在 class 中我们写了它只创建了一个到数据库的连接,但我们没有确保这个 class是一个单身人士,因此我们仍在创建到数据库的多个连接。一旦我们确定它是一个单例,那么繁忙的错误就会停止
我从中得到的是:
锁定意味着您有多个线程试图访问数据库,代码本质上不是线程安全的。
忙碌意味着您有一个线程正在等待另一个线程完成,您的代码是线程安全的,但您在使用数据库时看到争用。