Skip to content

Commit

Permalink
Log more when encountering sqlite errors and lower busy timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
tmolitor-stud-tu committed Oct 27, 2023
1 parent f7ee000 commit ed1ae68
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Monal/Classes/MLSQLite.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ -(id) initWithFile:(NSString*) dbFile
//some settings (e.g. truncate is faster than delete)
//this uses the private api because we have no thread local instance added to the threadData dictionary yet and we don't use a transaction either (and public apis check both)
//--> we must use the internal api because it does not call testThreadInstanceForQuery: testTransactionsForQuery:
sqlite3_busy_timeout(self->_database, 8000); //set the busy time as early as possible to make sure the pragma states don't trigger a retry too often
sqlite3_busy_timeout(self->_database, 2000); //set the busy time as early as possible to make sure the pragma states don't trigger a retry too often
while([self executeNonQuery:@"PRAGMA synchronous=NORMAL;" andArguments:@[] withException:NO] != YES)
DDLogError(@"Database locked, while calling 'PRAGMA synchronous=NORMAL;', retrying...");
while([self executeNonQuery:@"PRAGMA truncate;" andArguments:@[] withException:NO] != YES)
Expand Down Expand Up @@ -244,10 +244,11 @@ -(id) getColumn:(int) column ofStatement:(sqlite3_stmt*) statement

-(void) throwErrorForQuery:(NSString*) query andArguments:(NSArray*) args
{
int errcode = sqlite3_extended_errcode(self->_database);
NSString* error = [NSString stringWithUTF8String:sqlite3_errmsg(self->_database)];
DDLogError(@"SQLite Exception: %@ for query '%@' having params %@", error, query ? query : @"", args ? args : @[]);
DDLogError(@"SQLite Exception: %d %@ for query '%@' having params %@", errcode, error, query ? query : @"", args ? args : @[]);
DDLogError(@"currentTransactions: %@", currentTransactions);
@throw [NSException exceptionWithName:@"SQLite3Exception" reason:error userInfo:@{
@throw [NSException exceptionWithName:@"SQLite3Exception" reason:[NSString stringWithFormat:@"%d: %@", errcode, error] userInfo:@{
@"query": query ? query : [NSNull null],
@"args": args ? args : [NSNull null],
@"currentTransactions": currentTransactions,
Expand Down Expand Up @@ -310,7 +311,13 @@ -(BOOL) executeNonQuery:(NSString*) query andArguments:(NSArray *) args withExce
toReturn = YES;
else
{
DDLogVerbose(@"sqlite3_step(%@): %d --> %@", query, step, [[NSThread currentThread] threadDictionary]);
DDLogVerbose(@"sqlite3_step(%@): %d (%d) [%s] --> %@",
query,
step,
sqlite3_extended_errcode(self->_database),
sqlite3_errmsg(self->_database)
[[NSThread currentThread] threadDictionary]
);
if(throwException)
[self throwErrorForQuery:query andArguments:args];
toReturn = NO;
Expand Down

0 comments on commit ed1ae68

Please sign in to comment.