Skip to content

Commit 65a180d

Browse files
committed
fix: database maybe corrup
Move updating database of Applet from caller thread to Manager's thread.
1 parent 81ba22f commit 65a180d

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

panels/notification/common/dbaccessor.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <QDir>
1010
#include <QElapsedTimer>
1111
#include <QLoggingCategory>
12+
#include <QMutexLocker>
1213
#include <QSqlDatabase>
1314
#include <QSqlError>
1415
#include <QSqlQuery>
@@ -173,6 +174,7 @@ qint64 DBAccessor::addEntity(const NotifyEntity &entity)
173174
{
174175
BENCHMARK();
175176

177+
QMutexLocker locker(&m_mutex);
176178
QSqlQuery query(m_connection);
177179

178180
QString columns = QStringList{
@@ -224,6 +226,7 @@ void DBAccessor::updateEntityProcessedType(qint64 id, int processedType)
224226
{
225227
BENCHMARK();
226228

229+
QMutexLocker locker(&m_mutex);
227230
QSqlQuery query(m_connection);
228231

229232
QString cmd = QString("UPDATE %1 SET ProcessedType = :processed WHERE ID = :id").arg(TableName_v2);
@@ -240,6 +243,7 @@ NotifyEntity DBAccessor::fetchEntity(qint64 id)
240243
{
241244
BENCHMARK();
242245

246+
QMutexLocker locker(&m_mutex);
243247
QSqlQuery query(m_connection);
244248
QString cmd = QString("SELECT %1 FROM notifications2 WHERE ID = :id").arg(EntityFields.join(","));
245249
query.prepare(cmd);
@@ -260,6 +264,7 @@ int DBAccessor::fetchEntityCount(const QString &appName, int processedType) cons
260264
{
261265
BENCHMARK();
262266

267+
QMutexLocker locker(&m_mutex);
263268
QSqlQuery query(m_connection);
264269
if (appName == DataAccessor::AllApp()) {
265270
QString cmd = QString("SELECT COUNT(*) FROM notifications2 WHERE (ProcessedType = :processedType OR ProcessedType IS NULL)");
@@ -287,6 +292,7 @@ NotifyEntity DBAccessor::fetchLastEntity(const QString &appName, int processedTy
287292
{
288293
BENCHMARK();
289294

295+
QMutexLocker locker(&m_mutex);
290296
QSqlQuery query(m_connection);
291297
QString cmd =
292298
QString(
@@ -314,6 +320,7 @@ QList<NotifyEntity> DBAccessor::fetchEntities(const QString &appName, int proces
314320
{
315321
BENCHMARK();
316322

323+
QMutexLocker locker(&m_mutex);
317324
QSqlQuery query(m_connection);
318325
if (appName == DataAccessor::AllApp()) {
319326
if (maxCount >= 0) {
@@ -360,6 +367,7 @@ NotifyEntity DBAccessor::fetchLastEntity(uint notifyId)
360367
{
361368
BENCHMARK();
362369

370+
QMutexLocker locker(&m_mutex);
363371
QSqlQuery query(m_connection);
364372
QString cmd = QString("SELECT %1 FROM notifications2 WHERE notifyId = :notifyId ORDER BY CTime DESC LIMIT 1").arg(EntityFields.join(","));
365373
query.prepare(cmd);
@@ -383,6 +391,7 @@ QList<QString> DBAccessor::fetchApps(int maxCount) const
383391
{
384392
BENCHMARK();
385393

394+
QMutexLocker locker(&m_mutex);
386395
QSqlQuery query(m_connection);
387396
if (maxCount >= 0) {
388397
QString cmd("SELECT DISTINCT AppName FROM notifications2 ORDER BY CTime DESC LIMIT :limit");
@@ -413,6 +422,7 @@ void DBAccessor::removeEntity(qint64 id)
413422
{
414423
BENCHMARK();
415424

425+
QMutexLocker locker(&m_mutex);
416426
QSqlQuery query(m_connection);
417427

418428
QString cmd("DELETE FROM notifications2 WHERE ID = :id");
@@ -431,6 +441,7 @@ void DBAccessor::removeEntityByApp(const QString &appName)
431441
{
432442
BENCHMARK();
433443

444+
QMutexLocker locker(&m_mutex);
434445
QSqlQuery query(m_connection);
435446

436447
QString cmd("DELETE FROM notifications2 WHERE AppName = :appName");
@@ -449,6 +460,7 @@ void DBAccessor::clear()
449460
{
450461
BENCHMARK();
451462

463+
QMutexLocker locker(&m_mutex);
452464
QSqlQuery query(m_connection);
453465

454466
QString cmd("DELETE FROM notifications2");

panels/notification/common/dbaccessor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#pragma once
66

7+
#include <QMutex>
78
#include <QObject>
89
#include <QSqlDatabase>
910

@@ -50,6 +51,7 @@ class DBAccessor : public DataAccessor
5051
NotifyEntity parseEntity(const QSqlQuery &query);
5152

5253
private:
54+
mutable QMutex m_mutex;
5355
QSqlDatabase m_connection;
5456
QString m_key;
5557
};

panels/notification/server/notificationmanager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class NotificationManager : public QObject, public QDBusContext
3030
bool registerDbusService();
3131

3232
uint recordCount() const;
33-
void actionInvoked(qint64 id, uint bubbleId, const QString &actionKey);
34-
void notificationClosed(qint64 id, uint bubbleId, uint reason);
35-
void notificationReplaced(qint64 id);
33+
Q_INVOKABLE void actionInvoked(qint64 id, uint bubbleId, const QString &actionKey);
34+
Q_INVOKABLE void notificationClosed(qint64 id, uint bubbleId, uint reason);
35+
Q_INVOKABLE void notificationReplaced(qint64 id);
3636

3737
void removeNotification(qint64 id);
3838
void removeNotifications(const QString &appName);

panels/notification/server/notifyserverapplet.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ bool NotifyServerApplet::init()
6161

6262
void NotifyServerApplet::actionInvoked(qint64 id, uint bubbleId, const QString &actionKey)
6363
{
64-
m_manager->actionInvoked(id, bubbleId, actionKey);
64+
QMetaObject::invokeMethod(m_manager, "actionInvoked", Qt::AutoConnection, Q_ARG(qint64, id), Q_ARG(uint, bubbleId), Q_ARG(QString, actionKey));
6565
}
6666

6767
void NotifyServerApplet::notificationClosed(qint64 id, uint bubbleId, uint reason)
6868
{
69-
m_manager->notificationClosed(id, bubbleId, reason);
69+
QMetaObject::invokeMethod(m_manager, "notificationClosed", Qt::AutoConnection, Q_ARG(qint64, id), Q_ARG(uint, bubbleId), Q_ARG(uint, reason));
7070
}
7171

7272
void NotifyServerApplet::notificationReplaced(qint64 id)
7373
{
74-
m_manager->notificationReplaced(id);
74+
QMetaObject::invokeMethod(m_manager, "notificationReplaced", Qt::AutoConnection, Q_ARG(qint64, id));
7575
}
7676

7777
QVariant NotifyServerApplet::appValue(const QString &appId, int configItem)

0 commit comments

Comments
 (0)