get rid of the refcounted db connection

This commit is contained in:
Marco Martin 2011-04-28 21:43:36 +02:00
parent f358f3de6d
commit 88956b1539
3 changed files with 9 additions and 56 deletions

View File

@ -41,42 +41,6 @@
#include "storagethread_p.h" #include "storagethread_p.h"
class RefCountedDatabase
{
public:
void ref()
{
if (m_ref == 0) {
m_db = QSqlDatabase::addDatabase("QSQLITE", QString("plasma-storage-%1").arg((quintptr)this));
m_db.setDatabaseName(KStandardDirs::locateLocal("appdata", "plasma-storage2.db"));
}
//Q_ASSERT(db.isValid());
m_ref.ref();
}
bool deref()
{
//kill the database if the last one in use
bool last = !m_ref.deref();
if (last) {
m_db.close();
QString name = m_db.connectionName();
m_db = QSqlDatabase();
QSqlDatabase::removeDatabase(name);
}
return !last;
}
inline QSqlDatabase *database()
{
return &m_db;
}
private:
QSqlDatabase m_db;
QAtomicInt m_ref;
};
static QThreadStorage<RefCountedDatabase *> s_databasePool; static QThreadStorage<RefCountedDatabase *> s_databasePool;
@ -88,14 +52,6 @@ StorageJob::StorageJob(const QString& destination,
: ServiceJob(destination, operation, parameters, parent), : ServiceJob(destination, operation, parameters, parent),
m_clientName(destination) m_clientName(destination)
{ {
m_rdb = s_databasePool.localData();
if (m_rdb == 0) {
s_databasePool.setLocalData(new RefCountedDatabase);
m_rdb = s_databasePool.localData();
}
m_rdb->ref();
Plasma::StorageThread::self()->start(); Plasma::StorageThread::self()->start();
connect(Plasma::StorageThread::self(), SIGNAL(newResult(StorageJob *, const QVariant &)), this, SLOT(resultSlot(StorageJob *, const QVariant &))); connect(Plasma::StorageThread::self(), SIGNAL(newResult(StorageJob *, const QVariant &)), this, SLOT(resultSlot(StorageJob *, const QVariant &)));
qRegisterMetaType<StorageJob *>(); qRegisterMetaType<StorageJob *>();
@ -103,9 +59,6 @@ StorageJob::StorageJob(const QString& destination,
StorageJob::~StorageJob() StorageJob::~StorageJob()
{ {
if (!m_rdb->deref()) {
s_databasePool.setLocalData(0);
}
} }
void StorageJob::setData(const QVariantHash &data) void StorageJob::setData(const QVariantHash &data)
@ -127,9 +80,6 @@ QString StorageJob::clientName() const
void StorageJob::start() void StorageJob::start()
{ {
if (!m_rdb->database()->isOpen()) {
return;
}
//FIXME: QHASH //FIXME: QHASH
QMap<QString, QVariant> params = parameters(); QMap<QString, QVariant> params = parameters();
@ -139,9 +89,6 @@ void StorageJob::start()
valueGroup = "default"; valueGroup = "default";
} }
//kDebug() << operationName();
m_rdb->database()->transaction();
if (operationName() == "save") { if (operationName() == "save") {
QMetaObject::invokeMethod(Plasma::StorageThread::self(), "save", Qt::QueuedConnection, Q_ARG(StorageJob *, this), Q_ARG(const QVariantMap&, params)); QMetaObject::invokeMethod(Plasma::StorageThread::self(), "save", Qt::QueuedConnection, Q_ARG(StorageJob *, this), Q_ARG(const QVariantMap&, params));
@ -154,7 +101,6 @@ void StorageJob::start()
} else { } else {
setError(true); setError(true);
} }
m_rdb->database()->commit();
} }
void StorageJob::resultSlot(StorageJob *job, const QVariant &result) void StorageJob::resultSlot(StorageJob *job, const QVariant &result)

View File

@ -49,7 +49,6 @@ protected Q_SLOTS:
void resultSlot(StorageJob *job, const QVariant &result); void resultSlot(StorageJob *job, const QVariant &result);
private: private:
RefCountedDatabase *m_rdb;
QString m_clientName; QString m_clientName;
QVariantHash m_data; QVariantHash m_data;
}; };

View File

@ -25,7 +25,8 @@
#include <QSqlDriver> #include <QSqlDriver>
#include <QSqlRecord> #include <QSqlRecord>
#include "kdebug.h" #include <kdebug.h>
#include <kstandarddirs.h>
namespace Plasma namespace Plasma
{ {
@ -62,6 +63,11 @@ Plasma::StorageThread *StorageThread::self()
void StorageThread::initializeDb(StorageJob* caller) void StorageThread::initializeDb(StorageJob* caller)
{ {
if (!m_db.open()) {
m_db = QSqlDatabase::addDatabase("QSQLITE", QString("plasma-storage-%1").arg((quintptr)this));
m_db.setDatabaseName(KStandardDirs::locateLocal("appdata", "plasma-storage2.db"));
}
if (!m_db.open()) { if (!m_db.open()) {
kWarning() << "Unable to open the plasma storage cache database: " << m_db.lastError(); kWarning() << "Unable to open the plasma storage cache database: " << m_db.lastError();
} else if (!m_db.tables().contains(caller->clientName())) { } else if (!m_db.tables().contains(caller->clientName())) {
@ -72,6 +78,7 @@ void StorageThread::initializeDb(StorageJob* caller)
m_db.close(); m_db.close();
} }
} }
m_db.transaction();
} }
void StorageThread::save(StorageJob* caller, const QVariantMap &params) void StorageThread::save(StorageJob* caller, const QVariantMap &params)
@ -157,6 +164,7 @@ void StorageThread::save(StorageJob* caller, const QVariantMap &params)
query.bindValue(field, QVariant()); query.bindValue(field, QVariant());
} }
m_db.commit();
emit newResult(caller, true); emit newResult(caller, true);
} }