提交 eaac6f97 编写于 作者: 丁劲犇's avatar 丁劲犇 😸

* sloved a qt-5.1 --> 5.2 problem.

* sloved multi-thread db access problem. the databaseresource class has been
improved, for each thread, a thread-owned db connection will be cloned from main
db connection.
上级 7edc86fe
......@@ -461,7 +461,8 @@ int MainDialog::deal_current_message_block()
);
}
else
QMessageBox::information(this,tr("Failed!"),tr("Log in failed!"));
displayMessage(tr("Log in failed!"));
displayMessage(tr("Res = %1")
.arg(pApp->MsgUnion.msg_ClientLoginRsp.DoneCode)
);
......@@ -471,7 +472,7 @@ int MainDialog::deal_current_message_block()
if (pApp->MsgUnion.msg_UploadUserListRsp.DoneCode==0)
QMessageBox::information(this,tr("Succeed!"),tr("upload succeed!"));
else
QMessageBox::information(this,tr("Failed!"),tr("upload in Failed!"));
displayMessage(tr("Upload failed!"));
displayMessage(tr("Res = %1")
.arg(pApp->MsgUnion.msg_UploadUserListRsp.DoneCode)
);
......@@ -490,7 +491,7 @@ int MainDialog::deal_current_message_block()
ui->plainTextEdit_box_userids->setPlainText(strRes);
}
else
QMessageBox::information(this,tr("Failed!"),tr("download in Failed!"));
displayMessage(tr("Download failed!"));
displayMessage(tr("Res = %1")
.arg(pApp->MsgUnion.msg_DownloadUserListRsp.DoneCode)
);
......@@ -501,11 +502,7 @@ int MainDialog::deal_current_message_block()
if (pApp->MsgUnion.msg_ClientLogoutRsp.DoneCode==0)
QMessageBox::information(this,tr("Succeed!"),tr("log out succeed!"));
else
QMessageBox::information(this,tr("Failed!"),tr("download in Failed!"));
displayMessage(tr("Res = %1")
.arg(pApp->MsgUnion.msg_ClientLogoutRsp.DoneCode)
);
this->client->abort();
displayMessage(tr("Download failed!"));
}
else
{
......
#include "zp_clusterterm.h"
#include "zp_clusternode.h"
#include <assert.h>
#include <QStringList>
namespace ZP_Cluster{
using namespace std::placeholders;
zp_ClusterTerm::zp_ClusterTerm(QString name,QObject *parent ) :
......
......@@ -28,7 +28,67 @@ namespace ZPDatabase{
emit evt_Message(this,msg);
return QSqlDatabase();
}
return QSqlDatabase::database(strDBName);
//We need a thread owner db , instead of the main DB template
QString threadName = QString("%1_%2").arg(strDBName).arg((quint64)currentThreadId());
if (false==QSqlDatabase::contains(threadName))
{
QSqlDatabase db = QSqlDatabase::cloneDatabase(QSqlDatabase::database(strDBName),threadName);
if (db.open()==false)
{
QString msg = "Database:"+tr(" Connection name ")+threadName+
tr(" Can not be cloned from database %1.").arg(strDBName)+
tr(" Err String:") + db.lastError().text();
emit evt_Message(this,msg);
return QSqlDatabase();
}
m_ThreadsDB[strDBName].insert(threadName);
}
//Confirm the thread-owned db is still open
QSqlDatabase db = QSqlDatabase::database(threadName);
tagConnectionPara & para = m_dbNames[strDBName];
bool bNeedReconnect = false;
if (db.isOpen()==true)
{
if (para.testSQL.length())
{
QSqlQuery query(db);
query.exec(para.testSQL);
if (query.lastError().type()!=QSqlError::NoError)
{
QString msg = "Database:"+tr(" Connection ")+threadName+ tr(" confirm failed. MSG=");
msg += query.lastError().text();
emit evt_Message(this,msg);
bNeedReconnect = true;
}
}
if (bNeedReconnect==true)
{
db.close();
QSqlDatabase::removeDatabase(threadName);
}
}
else
bNeedReconnect = true;
if (bNeedReconnect==true)
{
db = QSqlDatabase::cloneDatabase(QSqlDatabase::database(strDBName),threadName);
if (db.open()==true)
{
QString msg = "Database:"+tr(" Connection ")+threadName+ tr(" Re-Established.");
emit evt_Message(this,msg);
}
else
{
QString msg = "Database:"+tr(" Connection name ")+threadName+
tr(" Can not be cloned from database %1.").arg(strDBName)+
tr(" Err String:") + db.lastError().text();
emit evt_Message(this,msg);
m_ThreadsDB[strDBName].remove(threadName);
return QSqlDatabase();
}
}
return db;
}
void DatabaseResource::remove_connections()
{
......@@ -37,9 +97,10 @@ namespace ZPDatabase{
QMutexLocker locker(&m_mutex_reg);
sets = currentDatabaseConnections();
}
foreach (QString name, sets.keys())
this->remove_connection(name);
{
this->remove_connection(name);
}
}
//!Remove Database
......@@ -54,7 +115,8 @@ namespace ZPDatabase{
QSqlDatabase::removeDatabase(strDBName);
QString msg = "Database:"+tr(" Connection removed ")+strDBName+ tr(" .");
emit evt_Message(this,msg);
RemoveTreadsConnections(strDBName);
m_ThreadsDB[strDBName].clear();
}
else
{
......@@ -64,6 +126,23 @@ namespace ZPDatabase{
m_dbNames.remove(strDBName) ;
}
void DatabaseResource::RemoveTreadsConnections(QString mainName)
{
if (m_ThreadsDB.contains(mainName))
{
QSet<QString> & sethreadNames = m_ThreadsDB[mainName];
foreach(QString str, sethreadNames)
{
QSqlDatabase db = QSqlDatabase::database(str);
if (db.isOpen()==true)
db.close();
QSqlDatabase::removeDatabase(str);
QString msg = "Database:"+tr(" Connection removed ")+str+ tr(" .");
emit evt_Message(this,msg);
}
}
}
/**
* @brief add a database connection resource
*
......
......@@ -7,12 +7,15 @@
#include <QString>
#include <QMutex>
#include <QThread>
#include <QSet>
namespace ZPDatabase{
/**
* @brief this class provide an database reource pool.In different thread, workers can get existing db connections
* immediately without re-creation operations. The working thread does not maintain db connections, instead of
* maintaining, it just using db resources from DatabaseResource
* Important!! When Ever a thread calls DatabaseResource::databse(), a thread-owned dbconnection
* will be cloned from main db connection. be careful, this class does not auto remove connections
* unless main connection has been removed by DatabaseResource::remove_connection(s)
* @class DatabaseResource databaseresource.h "ZoomPipeline_FuncSvr/database/databaseresource.h"
*/
class DatabaseResource : public QThread
......@@ -67,6 +70,8 @@ namespace ZPDatabase{
bool bTerm;
QMutex m_mutex_reg;
QMap <QString,tagConnectionPara> m_dbNames;
QMap <QString, QSet<QString> > m_ThreadsDB;
void RemoveTreadsConnections(QString mainName);
signals:
void evt_Message(QObject *,QString );
public slots:
......
......@@ -5,6 +5,7 @@
#include "st_cross_svr_msg.h"
#include <functional>
#include <QList>
#include <QStringList>
namespace ExampleServer{
using namespace std::placeholders;
st_client_table::st_client_table(
......
......@@ -13,9 +13,6 @@
<property name="windowTitle">
<string>ZPMainFrame</string>
</property>
<property name="toolTipDuration">
<number>-1</number>
</property>
<property name="iconSize">
<size>
<width>16</width>
......@@ -31,6 +28,9 @@
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<property name="toolTipDuration" stdset="0">
<number>-1</number>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
......@@ -995,7 +995,7 @@
<x>0</x>
<y>0</y>
<width>640</width>
<height>19</height>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menu_Control">
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册