SqliteException - SQLite Error 1: 'not an error'.
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
## SqliteException - SQLite Error 1: 'not an error'.
I am working on a C# .NET 6.0 project that uses Microsoft.Data.Sqlite version 7.0.5. We have several databases, where each database has a single table. Each database can have concurrent readers, and if a write is required, there is a single lock for writers such that only a single thread can write to the database at any given time.
The integration with Microsoft.Data.Sqlite is quite simplistic - however, there are many concurrent readers operating at a given time, and many writer threads may request access to write to the database.
```C#
public async Task Get(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new InvalidOperationException("Null or empty key can not be used to query");
var sql = $"SELECT [Value] FROM [{tableName}] WHERE [Key] = $key;";
string? value = null;
await using var connection = await OpenSqliteConnectionAsync();
try
{
await using var transaction = connection.BeginTransaction();
await using var command = new SqliteCommand(sql, connection, transaction);
command.Parameters.AddWithValue("$key", key);
await using (var reader = await command.ExecuteReaderAsync())
{
while (reader.Read())
{
value = reader.GetValue(0).ToString();
}
}
await transaction.CommitAsync();
return new value;
}
catch (Exception e)
{
logger.Error($"Failed to query \"{tableName}\" with message {e.Message}.", e);
throw;
}
}
private async Task OpenSqliteConnectionAsync()
{
var connection = new SqliteConnection(connectionString);
try
{
try
{
await connection.OpenAsync();
}
catch (SqliteException e)
{
logger.Error(
"Failed to open SQLite connection. " +
GetSqliteErrorMessageContextAsString(e),
e);
throw;
}
}
catch (Exception)
{
connection.Dispose();
throw;
}
return connection;
}
```
It has been observed in our test runs, that if there is enough load on the project, we have observed that an `SqliteException` in the `SqliteConnection.BeginTransaction()` call. It is not always guaranteed to emit exceptions, but we have observed the following issue:
```
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'not an error'.
```
Under what circumstance would this happen? I am struggling to reproduce this and the `not an error` part is throwing me off.
### Include stack traces
```
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'not an error'.
at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
at Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery()
at Microsoft.Data.Sqlite.SqliteConnectionExtensions.ExecuteNonQuery(SqliteConnection connection, String commandText, SqliteParameter[] parameters)
at Microsoft.Data.Sqlite.SqliteTransaction..ctor(SqliteConnection connection, IsolationLevel isolationLevel, Boolean deferred)
at Microsoft.Data.Sqlite.SqliteConnection.BeginTransaction(IsolationLevel isolationLevel, Boolean deferred)
```
### Include version information
Microsoft.Data.Sqlite version: 7.0.5
Target framework: .NET 6.0
Operating system: Windows Server 2019
Contributor guide
Assessment
This issue has not been assessed yet.