Akonadi

dbdeadlockcatcher.h
1/*
2 SPDX-FileCopyrightText: 2019 David Faure <faure@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "dbexception.h"
10
11namespace Akonadi
12{
13namespace Server
14{
15/**
16 This class catches DbDeadlockException (as emitted by QueryBuilder)
17 and retries execution of the method when it happens, as required by
18 SQL databases.
19*/
20class DbDeadlockCatcher
21{
22public:
23 template<typename Func>
24 explicit DbDeadlockCatcher(Func &&func)
25 {
26 callFunc(func, 0);
27 }
28
29private:
30 static const int MaxRecursion = 5;
31 template<typename Func>
32 void callFunc(Func &&func, int recursionCounter)
33 {
34 try {
35 func();
36 } catch (const DbDeadlockException &) {
37 if (recursionCounter == MaxRecursion) {
38 throw;
39 } else {
40 callFunc(func, ++recursionCounter); // recurse
41 }
42 }
43 }
44};
45
46} // namespace Server
47} // namespace Akonadi
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:49:57 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.