0

Is it safe to use default database connection from different threads? Like this:

bool upSafe(const QString &mig_to, const QString &mig_from) const {
  if (!QSqlDatabase::database().transaction()) {
    qCCritical(hfCoreMT) << "Failed init database transaction";
    return false;
  }

  if (!up(mig_to, mig_from)) {
    QSqlDatabase::database().rollback();
    return false;
  }

  return QSqlDatabase::database().commit();
}

In function up default QSQLQuery created and executed. Maybe some hints to the right pattern?

5
  • 2
    read this: code.qt.io/cgit/qt/qtbase.git/tree/dist/changes-5.11.0/… Commented May 30, 2018 at 6:25
  • What do you mean by 'different'? Commented May 30, 2018 at 6:29
  • @eyllanesc should I interpret is as: "It should be safe but till 5.11 there was a bug" ? Commented May 30, 2018 at 6:29
  • Your example shows single thread usage. Commented May 30, 2018 at 6:41
  • @Michał Walenciak usage is similar for other threads Commented May 30, 2018 at 7:01

1 Answer 1

2

QSqlDatabase (which represents one DB connection) is not reentrant. You can use a connection only from the thread you created it. If you need to perform queries from another thread you need to create another connection from that thread first.

Sign up to request clarification or add additional context in comments.

1 Comment

So the answer is "No". Default connection as any connectoin should be used only in thread where it have been opened.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.