Fix closing databases
This commit is contained in:
@@ -754,11 +754,10 @@ 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()) {
|
||||||
|
result.rows << SqlRow(q);
|
||||||
while (q.Next()) {
|
}
|
||||||
result.rows << SqlRow(q);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (QThread::currentThread() != thread() && QThread::currentThread() != backend_->thread()) {
|
if (QThread::currentThread() != thread() && QThread::currentThread() != backend_->thread()) {
|
||||||
@@ -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();
|
||||||
|
|||||||
@@ -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
|
||||||
QSqlDatabase db = QSqlDatabase::database(connection_id);
|
if (QSqlDatabase::connectionNames().contains(connection_id)) {
|
||||||
if (db.isOpen()) {
|
{
|
||||||
db.close();
|
QSqlDatabase db = QSqlDatabase::database(connection_id);
|
||||||
}
|
if (db.isOpen()) {
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ ConnectedDevice::ConnectedDevice(const QUrl &url, DeviceLister *lister, const QS
|
|||||||
}
|
}
|
||||||
|
|
||||||
ConnectedDevice::~ConnectedDevice() {
|
ConnectedDevice::~ConnectedDevice() {
|
||||||
|
backend_->Close();
|
||||||
backend_->deleteLater();
|
backend_->deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -80,27 +80,31 @@ void DeviceDatabaseBackend::Exit() {
|
|||||||
|
|
||||||
DeviceDatabaseBackend::DeviceList DeviceDatabaseBackend::GetAllDevices() {
|
DeviceDatabaseBackend::DeviceList DeviceDatabaseBackend::GetAllDevices() {
|
||||||
|
|
||||||
QMutexLocker l(db_->Mutex());
|
|
||||||
QSqlDatabase db(db_->Connect());
|
|
||||||
|
|
||||||
DeviceList ret;
|
DeviceList ret;
|
||||||
|
|
||||||
QSqlQuery q(db);
|
{
|
||||||
q.prepare("SELECT ROWID, unique_id, friendly_name, size, icon, transcode_mode, transcode_format FROM devices");
|
QMutexLocker l(db_->Mutex());
|
||||||
q.exec();
|
QSqlDatabase db(db_->Connect());
|
||||||
if (db_->CheckErrors(q)) return ret;
|
QSqlQuery q(db);
|
||||||
|
q.prepare("SELECT ROWID, unique_id, friendly_name, size, icon, transcode_mode, transcode_format FROM devices");
|
||||||
|
q.exec();
|
||||||
|
if (db_->CheckErrors(q)) return ret;
|
||||||
|
|
||||||
while (q.next()) {
|
while (q.next()) {
|
||||||
Device dev;
|
Device dev;
|
||||||
dev.id_ = q.value(0).toInt();
|
dev.id_ = q.value(0).toInt();
|
||||||
dev.unique_id_ = q.value(1).toString();
|
dev.unique_id_ = q.value(1).toString();
|
||||||
dev.friendly_name_ = q.value(2).toString();
|
dev.friendly_name_ = q.value(2).toString();
|
||||||
dev.size_ = q.value(3).toLongLong();
|
dev.size_ = q.value(3).toLongLong();
|
||||||
dev.icon_name_ = q.value(4).toString();
|
dev.icon_name_ = q.value(4).toString();
|
||||||
dev.transcode_mode_ = MusicStorage::TranscodeMode(q.value(5).toInt());
|
dev.transcode_mode_ = MusicStorage::TranscodeMode(q.value(5).toInt());
|
||||||
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -182,6 +182,7 @@ void DeviceManager::LoadAllDevices() {
|
|||||||
info->InitFromDb(device);
|
info->InitFromDb(device);
|
||||||
emit DeviceCreatedFromDB(info);
|
emit DeviceCreatedFromDB(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
backend_->Close();
|
backend_->Close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -195,19 +195,24 @@ QSqlQuery PlaylistBackend::GetPlaylistRows(int playlist) {
|
|||||||
|
|
||||||
QList<PlaylistItemPtr> PlaylistBackend::GetPlaylistItems(int playlist) {
|
QList<PlaylistItemPtr> PlaylistBackend::GetPlaylistItems(int playlist) {
|
||||||
|
|
||||||
QSqlQuery q = GetPlaylistRows(playlist);
|
|
||||||
// Note that as this only accesses the query, not the db, we don't need the mutex.
|
|
||||||
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
|
|
||||||
std::shared_ptr<NewSongFromQueryState> state_ptr(new NewSongFromQueryState());
|
|
||||||
QList<PlaylistItemPtr> playlistitems;
|
QList<PlaylistItemPtr> playlistitems;
|
||||||
while (q.next()) {
|
|
||||||
playlistitems << NewPlaylistItemFromQuery(SqlRow(q), state_ptr);
|
{
|
||||||
|
|
||||||
|
QSqlQuery q = GetPlaylistRows(playlist);
|
||||||
|
// Note that as this only accesses the query, not the db, we don't need the mutex.
|
||||||
|
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
|
||||||
|
std::shared_ptr<NewSongFromQueryState> state_ptr(new NewSongFromQueryState());
|
||||||
|
while (q.next()) {
|
||||||
|
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) {
|
||||||
|
|
||||||
QSqlQuery q = GetPlaylistRows(playlist);
|
SongList songs;
|
||||||
// Note that as this only accesses the query, not the db, we don't need the mutex.
|
|
||||||
if (db_->CheckErrors(q)) return QList<Song>();
|
{
|
||||||
|
|
||||||
|
QSqlQuery q = GetPlaylistRows(playlist);
|
||||||
|
// Note that as this only accesses the query, not the db, we don't need the mutex.
|
||||||
|
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
|
||||||
|
std::shared_ptr<NewSongFromQueryState> state_ptr(new NewSongFromQueryState());
|
||||||
|
while (q.next()) {
|
||||||
|
songs << NewSongFromQuery(SqlRow(q), state_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
// 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());
|
|
||||||
QList<Song> songs;
|
|
||||||
while (q.next()) {
|
|
||||||
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;
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user