Solved QMYSQL SIGSEGV
-
@SGaist
I will try this at weekend. Also i will test if MySQL and MariaDB has the same. The queries where made from the library. Thx for the time -
Hi
I connect to a localhost mysql server and itseems to work. When i has running in the background some sql queries and i do at the same time from other window with the same connection any queryit works.When i connect to a server via TCP it will crash.
Edit: Its also with localhost, but the time is a bit longer ;( -
Can you trigger also that when using an application that's not using the QSql** classes ?
-
I think i have found a solution ;-) Some Test will be good. I will enhance my tests.
The Idea i found by reading http://doc.qt.io/qt-5/threads-modules.htmlJust my Application worked since 2015 with QSQLITE without troubles. But some users will also use MySQL.
For the solution i have adapted a database manager for my requirement.
-
So you're using multiple threads to write to your database ?
-
@SGaist
Yes. I Import JSON Files with an Import Walter Thread. Wenn a File is copied in a Directory it will Import and created new sql entries.
Nur Why SQlite has no Troubles?
I will also Move long Working Stuff to threadOb they GUI you also can do some queries by clicking Buttons
-
I'd say pure luck.
Are you creating one connection per thread like suggested in the documentation ?
-
@SGaist
Yes. The DatabaseManager save the connections per thread into aQHash<QThread*, QHash<QString, QSqlDatabase> >
I have only one thread at a time wich write to the database. All others max. 4 to only select
Sorry for my "Austrian English" ;-) -
Can you show how your are setting up the connections ?
-
The first connection is in my first post
Than for every connection i callQSqlDatabase dbc = DatabaseManager::database("CN"); QSqlQuery query(dbc) . .
.
#include "databasemanager.h" #include <QSqlDatabase> #include <QMutexLocker> #include <QThread> #include <QSqlError> #include <QDebug> QMutex DatabaseManager::s_databaseMutex; QHash<QThread*, QHash<QString, QSqlDatabase> > DatabaseManager::s_instances; QSqlDatabase DatabaseManager::database(const QString& connectionName) { QMutexLocker locker(&s_databaseMutex); QThread *thread = QThread::currentThread(); // if we have a connection for this thread, return it QHash<QThread*, QHash<QString, QSqlDatabase> >::Iterator it_thread = s_instances.find(thread); if (it_thread != s_instances.end()) { QHash<QString, QSqlDatabase>::iterator it_conn = it_thread.value().find(connectionName); if (it_conn != it_thread.value().end()) { QSqlDatabase connection = it_conn.value(); // qDebug() << "Function Name: " << Q_FUNC_INFO << " found SQL connection instances Thread: " << thread->currentThreadId() << " Name: " << connectionName; if (connection.isValid()) return it_conn.value(); } } // otherwise, create a new connection for this thread QSqlDatabase connection = QSqlDatabase::cloneDatabase( QSqlDatabase::database(connectionName), QString("%1_%2").arg(connectionName).arg((int)thread)); // open the database connection // initialize the database connection if (!connection.open()) { // Todo: Exeption Handling qCritical() << "Function Name: " << Q_FUNC_INFO << connection.lastError().text(); return connection; } qDebug() << "Function Name: " << Q_FUNC_INFO << " new SQL connection instances Thread: " << thread->currentThreadId() << " Name: " << connectionName; s_instances[thread][connectionName] = connection; return connection; }
#ifndef DATABASEMANAGER_H #define DATABASEMANAGER_H #include "qrkcore_global.h" #include <QMutex> #include <QHash> #include <QSqlDatabase> class QThread; class QRK_EXPORT DatabaseManager { public: static QSqlDatabase database(const QString& connectionName = QLatin1String(QSqlDatabase::defaultConnection)); private: static QMutex s_databaseMutex; static QHash<QThread*, QHash<QString, QSqlDatabase>> s_instances; }; #endif // DATABASEMANAGER_H
I found the code and adapted it for my requirement.
-
So. I made some tests with create more than 1000 receipts and do concurrent imports with 10000 Products and do some select. I seems it works now without any troubles localhost and IP also over slow connection over the internet.
i think it fix my problem.
lg chris