Fix closing databases

This commit is contained in:
Jonas Kvinge
2019-07-25 17:56:28 +02:00
parent 41484f8673
commit 81caec99b7
10 changed files with 82 additions and 63 deletions

View File

@@ -754,12 +754,11 @@ CollectionModel::QueryResult CollectionModel::RunQuery(CollectionItem *parent) {
// Execute the query // Execute the query
QMutexLocker l(backend_->db()->Mutex()); QMutexLocker l(backend_->db()->Mutex());
if (backend_->ExecQuery(&q)) {
if (!backend_->ExecQuery(&q)) return result;
while (q.Next()) { while (q.Next()) {
result.rows << SqlRow(q); result.rows << SqlRow(q);
} }
}
if (QThread::currentThread() != thread() && QThread::currentThread() != backend_->thread()) { if (QThread::currentThread() != thread() && QThread::currentThread() != backend_->thread()) {
backend_->Close(); backend_->Close();
@@ -811,6 +810,10 @@ void CollectionModel::ResetAsync() {
void CollectionModel::ResetAsyncQueryFinished(QFuture<CollectionModel::QueryResult> future) { void CollectionModel::ResetAsyncQueryFinished(QFuture<CollectionModel::QueryResult> future) {
if (QThread::currentThread() != thread() && QThread::currentThread() != backend_->thread()) {
backend_->Close();
}
const struct QueryResult result = future.result(); const struct QueryResult result = future.result();
BeginReset(); BeginReset();

View File

@@ -250,13 +250,10 @@ Database::~Database() {
QMutexLocker l(&connect_mutex_); QMutexLocker l(&connect_mutex_);
for (QString connection : connections_) { for (QString &connection_id : QSqlDatabase::connectionNames()) {
qLog(Error) << connection << "still open!"; qLog(Error) << "Connection" << connection_id << "is still open!";
} }
if (!connections_.isEmpty())
qLog(Error) << connections_.count() << "connections still open!";
if (sFTSTokenizer) if (sFTSTokenizer)
delete sFTSTokenizer; delete sFTSTokenizer;
@@ -288,18 +285,18 @@ QSqlDatabase Database::Connect() {
const QString connection_id = QString("%1_thread_%2").arg(connection_id_).arg(reinterpret_cast<quint64>(QThread::currentThread())); const QString connection_id = QString("%1_thread_%2").arg(connection_id_).arg(reinterpret_cast<quint64>(QThread::currentThread()));
if (!connections_.contains(connection_id)) {
//qLog(Debug) << "Opened database with connection id" << connection_id;
connections_ << connection_id;
}
// Try to find an existing connection for this thread // Try to find an existing connection for this thread
QSqlDatabase db = QSqlDatabase::database(connection_id); QSqlDatabase db;
if (QSqlDatabase::connectionNames().contains(connection_id)) {
db = QSqlDatabase::database(connection_id);
}
else {
db = QSqlDatabase::addDatabase("QSQLITE", connection_id);
}
if (db.isOpen()) { if (db.isOpen()) {
return db; return db;
} }
//qLog(Debug) << "Opened database with connection id" << connection_id;
db = QSqlDatabase::addDatabase("QSQLITE", connection_id);
if (!injected_database_name_.isNull()) if (!injected_database_name_.isNull())
db.setDatabaseName(injected_database_name_); db.setDatabaseName(injected_database_name_);
@@ -389,14 +386,15 @@ void Database::Close() {
const QString connection_id = QString("%1_thread_%2").arg(connection_id_).arg(reinterpret_cast<quint64>(QThread::currentThread())); const QString connection_id = QString("%1_thread_%2").arg(connection_id_).arg(reinterpret_cast<quint64>(QThread::currentThread()));
// Try to find an existing connection for this thread // Try to find an existing connection for this thread
if (QSqlDatabase::connectionNames().contains(connection_id)) {
{
QSqlDatabase db = QSqlDatabase::database(connection_id); QSqlDatabase db = QSqlDatabase::database(connection_id);
if (db.isOpen()) { if (db.isOpen()) {
db.close(); db.close();
}
if (connections_.contains(connection_id)) {
//qLog(Debug) << "Closed database with connection id" << connection_id; //qLog(Debug) << "Closed database with connection id" << connection_id;
connections_.removeAll(connection_id); }
}
QSqlDatabase::removeDatabase(connection_id);
} }
} }

View File

@@ -118,7 +118,6 @@ class Database : public QObject {
// This ID makes the QSqlDatabase name unique to the object as well as the thread // This ID makes the QSqlDatabase name unique to the object as well as the thread
int connection_id_; int connection_id_;
QStringList connections_;
static QMutex sNextConnectionIdMutex; static QMutex sNextConnectionIdMutex;
static int sNextConnectionId; static int sNextConnectionId;

View File

@@ -70,6 +70,7 @@ ConnectedDevice::ConnectedDevice(const QUrl &url, DeviceLister *lister, const QS
} }
ConnectedDevice::~ConnectedDevice() { ConnectedDevice::~ConnectedDevice() {
backend_->Close();
backend_->deleteLater(); backend_->deleteLater();
} }

View File

@@ -80,11 +80,11 @@ void DeviceDatabaseBackend::Exit() {
DeviceDatabaseBackend::DeviceList DeviceDatabaseBackend::GetAllDevices() { DeviceDatabaseBackend::DeviceList DeviceDatabaseBackend::GetAllDevices() {
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
DeviceList ret; DeviceList ret;
{
QMutexLocker l(db_->Mutex());
QSqlDatabase db(db_->Connect());
QSqlQuery q(db); QSqlQuery q(db);
q.prepare("SELECT ROWID, unique_id, friendly_name, size, icon, transcode_mode, transcode_format FROM devices"); q.prepare("SELECT ROWID, unique_id, friendly_name, size, icon, transcode_mode, transcode_format FROM devices");
q.exec(); q.exec();
@@ -101,6 +101,10 @@ DeviceDatabaseBackend::DeviceList DeviceDatabaseBackend::GetAllDevices() {
dev.transcode_format_ = Song::FileType(q.value(6).toInt()); dev.transcode_format_ = Song::FileType(q.value(6).toInt());
ret << dev; ret << dev;
} }
}
Close();
return ret; return ret;
} }

View File

@@ -182,6 +182,7 @@ void DeviceManager::LoadAllDevices() {
info->InitFromDb(device); info->InitFromDb(device);
emit DeviceCreatedFromDB(info); emit DeviceCreatedFromDB(info);
} }
backend_->Close(); backend_->Close();
} }

View File

@@ -219,6 +219,8 @@ void GPodDevice::WriteDatabase(bool success) {
} }
} }
backend_->Close();
songs_to_add_.clear(); songs_to_add_.clear();
songs_to_remove_.clear(); songs_to_remove_.clear();
db_busy_.unlock(); db_busy_.unlock();

View File

@@ -184,6 +184,8 @@ void MtpDevice::FinishCopy(bool success) {
songs_to_add_.clear(); songs_to_add_.clear();
songs_to_remove_.clear(); songs_to_remove_.clear();
backend_->Close();
db_busy_.unlock(); db_busy_.unlock();
ConnectedDevice::FinishCopy(success); ConnectedDevice::FinishCopy(success);

View File

@@ -195,19 +195,24 @@ QSqlQuery PlaylistBackend::GetPlaylistRows(int playlist) {
QList<PlaylistItemPtr> PlaylistBackend::GetPlaylistItems(int playlist) { QList<PlaylistItemPtr> PlaylistBackend::GetPlaylistItems(int playlist) {
QList<PlaylistItemPtr> playlistitems;
{
QSqlQuery q = GetPlaylistRows(playlist); QSqlQuery q = GetPlaylistRows(playlist);
// Note that as this only accesses the query, not the db, we don't need the mutex. // Note that as this only accesses the query, not the db, we don't need the mutex.
if (db_->CheckErrors(q)) return QList<PlaylistItemPtr>(); if (db_->CheckErrors(q)) return QList<PlaylistItemPtr>();
// it's probable that we'll have a few songs associated with the same CUE so we're caching results of parsing CUEs // it's probable that we'll have a few songs associated with the same CUE so we're caching results of parsing CUEs
std::shared_ptr<NewSongFromQueryState> state_ptr(new NewSongFromQueryState()); std::shared_ptr<NewSongFromQueryState> state_ptr(new NewSongFromQueryState());
QList<PlaylistItemPtr> playlistitems;
while (q.next()) { while (q.next()) {
playlistitems << NewPlaylistItemFromQuery(SqlRow(q), state_ptr); playlistitems << NewPlaylistItemFromQuery(SqlRow(q), state_ptr);
} }
}
if (QThread::currentThread() != thread() && QThread::currentThread() != qApp->thread()) { if (QThread::currentThread() != thread() && QThread::currentThread() != qApp->thread()) {
db_->Close(); Close();
} }
return playlistitems; return playlistitems;
@@ -216,19 +221,24 @@ QList<PlaylistItemPtr> PlaylistBackend::GetPlaylistItems(int playlist) {
QList<Song> PlaylistBackend::GetPlaylistSongs(int playlist) { QList<Song> PlaylistBackend::GetPlaylistSongs(int playlist) {
SongList songs;
{
QSqlQuery q = GetPlaylistRows(playlist); QSqlQuery q = GetPlaylistRows(playlist);
// Note that as this only accesses the query, not the db, we don't need the mutex. // Note that as this only accesses the query, not the db, we don't need the mutex.
if (db_->CheckErrors(q)) return QList<Song>(); if (db_->CheckErrors(q)) return QList<Song>();
// it's probable that we'll have a few songs associated with the same CUE so we're caching results of parsing CUEs // it's probable that we'll have a few songs associated with the same CUE so we're caching results of parsing CUEs
std::shared_ptr<NewSongFromQueryState> state_ptr(new NewSongFromQueryState()); std::shared_ptr<NewSongFromQueryState> state_ptr(new NewSongFromQueryState());
QList<Song> songs;
while (q.next()) { while (q.next()) {
songs << NewSongFromQuery(SqlRow(q), state_ptr); songs << NewSongFromQuery(SqlRow(q), state_ptr);
} }
}
if (QThread::currentThread() != thread() && QThread::currentThread() != qApp->thread()) { if (QThread::currentThread() != thread() && QThread::currentThread() != qApp->thread()) {
db_->Close(); Close();
} }
return songs; return songs;

View File

@@ -353,7 +353,6 @@ QString FileTypeItemDelegate::displayText(const QVariant &value, const QLocale &
} }
QWidget *TextItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QWidget *TextItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
return new QLineEdit(parent); return new QLineEdit(parent);
} }