mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
update ModelBase locking code (TODO: remove dependence on MultithreadContainer altogether), fix issue with pending task get id
This commit is contained in:
parent
4a5b043fb5
commit
230d77a1e4
@ -1,5 +1,5 @@
|
|||||||
CREATE TABLE `pending_tasks` (
|
CREATE TABLE `pending_tasks` (
|
||||||
`id` int UNSIGNED NOT NULL,
|
`id` int UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||||
`user_id` int UNSIGNED DEFAULT '0',
|
`user_id` int UNSIGNED DEFAULT '0',
|
||||||
`request` varbinary(2048) NOT NULL,
|
`request` varbinary(2048) NOT NULL,
|
||||||
`created` datetime NOT NULL,
|
`created` datetime NOT NULL,
|
||||||
|
|||||||
@ -42,7 +42,8 @@ namespace model {
|
|||||||
{
|
{
|
||||||
//printf("ModelBase::insertIntoDB with table: %s\n", getTableName());
|
//printf("ModelBase::insertIntoDB with table: %s\n", getTableName());
|
||||||
auto cm = ConnectionManager::getInstance();
|
auto cm = ConnectionManager::getInstance();
|
||||||
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
Poco::ScopedLock<Poco::Mutex> _poco_lock(mWorkMutex);
|
||||||
|
UNIQUE_LOCK;
|
||||||
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
||||||
Poco::Data::Statement insert = _insertIntoDB(session);
|
Poco::Data::Statement insert = _insertIntoDB(session);
|
||||||
|
|
||||||
@ -85,7 +86,8 @@ namespace model {
|
|||||||
bool ModelBase::isExistInDB()
|
bool ModelBase::isExistInDB()
|
||||||
{
|
{
|
||||||
auto cm = ConnectionManager::getInstance();
|
auto cm = ConnectionManager::getInstance();
|
||||||
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
Poco::ScopedLock<Poco::Mutex> _poco_lock(mWorkMutex);
|
||||||
|
UNIQUE_LOCK;
|
||||||
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
||||||
|
|
||||||
Poco::Data::Statement select = _loadIdFromDB(session);
|
Poco::Data::Statement select = _loadIdFromDB(session);
|
||||||
@ -102,7 +104,8 @@ namespace model {
|
|||||||
|
|
||||||
bool ModelBase::deleteFromDB()
|
bool ModelBase::deleteFromDB()
|
||||||
{
|
{
|
||||||
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
Poco::ScopedLock<Poco::Mutex> _poco_lock(mWorkMutex);
|
||||||
|
UNIQUE_LOCK;
|
||||||
if (mID == 0) {
|
if (mID == 0) {
|
||||||
addError(new Error(getTableName(), "id is zero, couldn't delete from db"));
|
addError(new Error(getTableName(), "id is zero, couldn't delete from db"));
|
||||||
return false;
|
return false;
|
||||||
@ -116,10 +119,8 @@ namespace model {
|
|||||||
return deleteStmt.execute() == 1;
|
return deleteStmt.execute() == 1;
|
||||||
}
|
}
|
||||||
catch (Poco::Exception& ex) {
|
catch (Poco::Exception& ex) {
|
||||||
lock();
|
|
||||||
addError(new ParamError(getTableName(), "mysql error by delete", ex.displayText().data()));
|
addError(new ParamError(getTableName(), "mysql error by delete", ex.displayText().data()));
|
||||||
addError(new ParamError(getTableName(), "id: ", mID));
|
addError(new ParamError(getTableName(), "id: ", mID));
|
||||||
unlock();
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -63,8 +63,8 @@ namespace model {
|
|||||||
|
|
||||||
bool deleteFromDB();
|
bool deleteFromDB();
|
||||||
|
|
||||||
inline void setID(int id) { lock(); mID = id; unlock(); }
|
inline void setID(int id) { UNIQUE_LOCK; mID = id; }
|
||||||
inline int getID() { lock(); int id = mID; unlock(); return id; }
|
inline int getID() const { SHARED_LOCK; return mID; }
|
||||||
|
|
||||||
static Poco::DateTime parseElopageDate(std::string dateString);
|
static Poco::DateTime parseElopageDate(std::string dateString);
|
||||||
|
|
||||||
@ -95,7 +95,8 @@ namespace model {
|
|||||||
size_t ModelBase::loadFromDB(const std::string& fieldName, const T& fieldValue)
|
size_t ModelBase::loadFromDB(const std::string& fieldName, const T& fieldValue)
|
||||||
{
|
{
|
||||||
auto cm = ConnectionManager::getInstance();
|
auto cm = ConnectionManager::getInstance();
|
||||||
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
Poco::ScopedLock<Poco::Mutex> _poco_lock(mWorkMutex);
|
||||||
|
UNIQUE_LOCK;
|
||||||
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
||||||
Poco::Data::Statement select = _loadFromDB(session, fieldName);
|
Poco::Data::Statement select = _loadFromDB(session, fieldName);
|
||||||
select, Poco::Data::Keywords::useRef(fieldValue);
|
select, Poco::Data::Keywords::useRef(fieldValue);
|
||||||
@ -115,7 +116,7 @@ namespace model {
|
|||||||
size_t ModelBase::countColumns(const std::string& fieldName, const T& fieldValue)
|
size_t ModelBase::countColumns(const std::string& fieldName, const T& fieldValue)
|
||||||
{
|
{
|
||||||
auto cm = ConnectionManager::getInstance();
|
auto cm = ConnectionManager::getInstance();
|
||||||
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
//Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
||||||
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
||||||
Poco::Data::Statement select(session);
|
Poco::Data::Statement select(session);
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
@ -140,7 +141,7 @@ namespace model {
|
|||||||
bool ModelBase::isExistInDB(const std::string& fieldName, const T& fieldValue)
|
bool ModelBase::isExistInDB(const std::string& fieldName, const T& fieldValue)
|
||||||
{
|
{
|
||||||
auto cm = ConnectionManager::getInstance();
|
auto cm = ConnectionManager::getInstance();
|
||||||
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
//Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
||||||
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
||||||
Poco::Data::Statement select(session);
|
Poco::Data::Statement select(session);
|
||||||
int id;
|
int id;
|
||||||
@ -167,7 +168,8 @@ namespace model {
|
|||||||
{
|
{
|
||||||
//printf("ModelBase::loadFromDB multi\n");
|
//printf("ModelBase::loadFromDB multi\n");
|
||||||
std::vector<Tuple> results;
|
std::vector<Tuple> results;
|
||||||
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
Poco::ScopedLock<Poco::Mutex> _poco_lock(mWorkMutex);
|
||||||
|
UNIQUE_LOCK;
|
||||||
//return results;
|
//return results;
|
||||||
if (expectedResults > 0) {
|
if (expectedResults > 0) {
|
||||||
results.reserve(expectedResults);
|
results.reserve(expectedResults);
|
||||||
@ -194,8 +196,8 @@ namespace model {
|
|||||||
std::vector<Tuple> ModelBase::loadAllFromDB()
|
std::vector<Tuple> ModelBase::loadAllFromDB()
|
||||||
{
|
{
|
||||||
std::vector<Tuple> results;
|
std::vector<Tuple> results;
|
||||||
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
Poco::ScopedLock<Poco::Mutex> _poco_lock(mWorkMutex);
|
||||||
|
UNIQUE_LOCK;
|
||||||
auto cm = ConnectionManager::getInstance();
|
auto cm = ConnectionManager::getInstance();
|
||||||
try {
|
try {
|
||||||
Poco::Data::Statement select = _loadAllFromDB(cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER));
|
Poco::Data::Statement select = _loadAllFromDB(cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER));
|
||||||
@ -221,7 +223,8 @@ namespace model {
|
|||||||
std::vector<Tuple> ModelBase::loadFromDB(const std::vector<std::string>& fieldNames, const std::vector<WhereFieldType>& fieldValues, MysqlConditionType conditionType/* = MYSQL_CONDITION_AND*/, int expectedResults/* = 0*/)
|
std::vector<Tuple> ModelBase::loadFromDB(const std::vector<std::string>& fieldNames, const std::vector<WhereFieldType>& fieldValues, MysqlConditionType conditionType/* = MYSQL_CONDITION_AND*/, int expectedResults/* = 0*/)
|
||||||
{
|
{
|
||||||
std::vector<Tuple> results;
|
std::vector<Tuple> results;
|
||||||
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
Poco::ScopedLock<Poco::Mutex> _poco_lock(mWorkMutex);
|
||||||
|
UNIQUE_LOCK;
|
||||||
if (fieldNames.size() != fieldValues.size() || fieldNames.size() <= 1) {
|
if (fieldNames.size() != fieldValues.size() || fieldNames.size() <= 1) {
|
||||||
lock();
|
lock();
|
||||||
addError(new Error(getTableName(), "fieldNames and fieldValues size don't match or smaller as 1"));
|
addError(new Error(getTableName(), "fieldNames and fieldValues size don't match or smaller as 1"));
|
||||||
@ -244,12 +247,10 @@ namespace model {
|
|||||||
resultCount = select.execute();
|
resultCount = select.execute();
|
||||||
}
|
}
|
||||||
catch (Poco::Exception& ex) {
|
catch (Poco::Exception& ex) {
|
||||||
lock();
|
|
||||||
addError(new ParamError(getTableName(), "mysql error by multi selecting", ex.displayText().data()));
|
addError(new ParamError(getTableName(), "mysql error by multi selecting", ex.displayText().data()));
|
||||||
for (auto it = fieldNames.begin(); it != fieldNames.end(); it++) {
|
for (auto it = fieldNames.begin(); it != fieldNames.end(); it++) {
|
||||||
addError(new ParamError(getTableName(), "field name for select: ", (*it).data()));
|
addError(new ParamError(getTableName(), "field name for select: ", (*it).data()));
|
||||||
}
|
}
|
||||||
unlock();
|
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
@ -258,7 +259,8 @@ namespace model {
|
|||||||
size_t ModelBase::loadFromDB(const std::vector<std::string>& fieldNames, const T1& field1Value, const T2& field2Value, MysqlConditionType conditionType/* = MYSQL_CONDITION_AND*/)
|
size_t ModelBase::loadFromDB(const std::vector<std::string>& fieldNames, const T1& field1Value, const T2& field2Value, MysqlConditionType conditionType/* = MYSQL_CONDITION_AND*/)
|
||||||
{
|
{
|
||||||
auto cm = ConnectionManager::getInstance();
|
auto cm = ConnectionManager::getInstance();
|
||||||
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
Poco::ScopedLock<Poco::Mutex> _poco_lock(mWorkMutex);
|
||||||
|
UNIQUE_LOCK;
|
||||||
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
||||||
Poco::Data::Statement select = _loadFromDB(session, fieldNames, conditionType);
|
Poco::Data::Statement select = _loadFromDB(session, fieldNames, conditionType);
|
||||||
select, Poco::Data::Keywords::useRef(field1Value), Poco::Data::Keywords::useRef(field2Value);
|
select, Poco::Data::Keywords::useRef(field1Value), Poco::Data::Keywords::useRef(field2Value);
|
||||||
@ -268,7 +270,6 @@ namespace model {
|
|||||||
resultCount = select.execute();
|
resultCount = select.execute();
|
||||||
}
|
}
|
||||||
catch (Poco::Exception& ex) {
|
catch (Poco::Exception& ex) {
|
||||||
lock();
|
|
||||||
addError(new ParamError(getTableName(), "mysql error by selecting, maybe more than one result?", ex.displayText()));
|
addError(new ParamError(getTableName(), "mysql error by selecting, maybe more than one result?", ex.displayText()));
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (auto it = fieldNames.begin(); it != fieldNames.end(); it++) {
|
for (auto it = fieldNames.begin(); it != fieldNames.end(); it++) {
|
||||||
@ -276,7 +277,6 @@ namespace model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//addError(new ParamError(getTableName(), "field name for select: ", fieldName.data()));
|
//addError(new ParamError(getTableName(), "field name for select: ", fieldName.data()));
|
||||||
unlock();
|
|
||||||
}
|
}
|
||||||
return resultCount;
|
return resultCount;
|
||||||
}
|
}
|
||||||
@ -286,7 +286,8 @@ namespace model {
|
|||||||
size_t ModelBase::updateIntoDB(const std::string& fieldName, const T& fieldValue)
|
size_t ModelBase::updateIntoDB(const std::string& fieldName, const T& fieldValue)
|
||||||
{
|
{
|
||||||
auto cm = ConnectionManager::getInstance();
|
auto cm = ConnectionManager::getInstance();
|
||||||
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
Poco::ScopedLock<Poco::Mutex> _poco_lock(mWorkMutex);
|
||||||
|
UNIQUE_LOCK;
|
||||||
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
||||||
Poco::Data::Statement update(session);
|
Poco::Data::Statement update(session);
|
||||||
|
|
||||||
@ -303,10 +304,9 @@ namespace model {
|
|||||||
resultCount = update.execute();
|
resultCount = update.execute();
|
||||||
}
|
}
|
||||||
catch (Poco::Exception& ex) {
|
catch (Poco::Exception& ex) {
|
||||||
lock();
|
|
||||||
addError(new ParamError(getTableName(), "mysql error by update", ex.displayText().data()));
|
addError(new ParamError(getTableName(), "mysql error by update", ex.displayText().data()));
|
||||||
addError(new ParamError(getTableName(), "field name for update: ", fieldName.data()));
|
addError(new ParamError(getTableName(), "field name for update: ", fieldName.data()));
|
||||||
unlock();
|
|
||||||
}
|
}
|
||||||
return resultCount;
|
return resultCount;
|
||||||
|
|
||||||
@ -315,7 +315,8 @@ namespace model {
|
|||||||
size_t ModelBase::updateIntoDB(std::string fieldNames[2], const T1& fieldValue1, const T2& fieldValue2)
|
size_t ModelBase::updateIntoDB(std::string fieldNames[2], const T1& fieldValue1, const T2& fieldValue2)
|
||||||
{
|
{
|
||||||
auto cm = ConnectionManager::getInstance();
|
auto cm = ConnectionManager::getInstance();
|
||||||
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
Poco::ScopedLock<Poco::Mutex> _poco_lock(mWorkMutex);
|
||||||
|
UNIQUE_LOCK;
|
||||||
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
||||||
Poco::Data::Statement update(session);
|
Poco::Data::Statement update(session);
|
||||||
|
|
||||||
@ -339,13 +340,10 @@ namespace model {
|
|||||||
resultCount = update.execute();
|
resultCount = update.execute();
|
||||||
}
|
}
|
||||||
catch (Poco::Exception& ex) {
|
catch (Poco::Exception& ex) {
|
||||||
lock();
|
|
||||||
addError(new ParamError(getTableName(), "mysql error by update 2", ex.displayText()));
|
addError(new ParamError(getTableName(), "mysql error by update 2", ex.displayText()));
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
addError(new ParamError(getTableName(), "field name for update: ", fieldNames[i]));
|
addError(new ParamError(getTableName(), "field name for update: ", fieldNames[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
unlock();
|
|
||||||
}
|
}
|
||||||
return resultCount;
|
return resultCount;
|
||||||
}
|
}
|
||||||
@ -353,7 +351,8 @@ namespace model {
|
|||||||
size_t ModelBase::updateIntoDB(std::string fieldNames[3], const T1& fieldValue1, const T2& fieldValue2, const T3& fieldValue3)
|
size_t ModelBase::updateIntoDB(std::string fieldNames[3], const T1& fieldValue1, const T2& fieldValue2, const T3& fieldValue3)
|
||||||
{
|
{
|
||||||
auto cm = ConnectionManager::getInstance();
|
auto cm = ConnectionManager::getInstance();
|
||||||
Poco::ScopedLock<Poco::Mutex> _lock(mWorkMutex);
|
Poco::ScopedLock<Poco::Mutex> _poco_lock(mWorkMutex);
|
||||||
|
UNIQUE_LOCK;
|
||||||
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
|
||||||
Poco::Data::Statement update(session);
|
Poco::Data::Statement update(session);
|
||||||
|
|
||||||
@ -377,13 +376,11 @@ namespace model {
|
|||||||
resultCount = update.execute();
|
resultCount = update.execute();
|
||||||
}
|
}
|
||||||
catch (Poco::Exception& ex) {
|
catch (Poco::Exception& ex) {
|
||||||
lock();
|
|
||||||
addError(new ParamError(getTableName(), "mysql error by update 3", ex.displayText()));
|
addError(new ParamError(getTableName(), "mysql error by update 3", ex.displayText()));
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
addError(new ParamError(getTableName(), "field name for update: ", fieldNames[i]));
|
addError(new ParamError(getTableName(), "field name for update: ", fieldNames[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
unlock();
|
|
||||||
}
|
}
|
||||||
return resultCount;
|
return resultCount;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -84,7 +84,17 @@ namespace model
|
|||||||
}
|
}
|
||||||
return "<invalid>";
|
return "<invalid>";
|
||||||
}
|
}
|
||||||
|
/*enum TaskType {
|
||||||
|
TASK_TYPE_NONE = 0,
|
||||||
|
TASK_TYPE_GROUP_CREATE = 1,
|
||||||
|
TASK_TYPE_GROUP_ADD_MEMBER = 2,
|
||||||
|
TASK_TYPE_CREATION = 10,
|
||||||
|
TASK_TYPE_TRANSFER = 11,
|
||||||
|
TASK_TYPE_HEDERA_TOPIC_CREATE = 20,
|
||||||
|
TASK_TYPE_HEDERA_TOPIC_MESSAGE = 21,
|
||||||
|
TASK_TYPE_HEDERA_ACCOUNT_CREATE = 25,
|
||||||
|
|
||||||
|
};*/
|
||||||
bool PendingTask::isGradidoTransaction(TaskType type)
|
bool PendingTask::isGradidoTransaction(TaskType type)
|
||||||
{
|
{
|
||||||
if (type && type < TASK_TYPE_HEDERA_TOPIC_CREATE)
|
if (type && type < TASK_TYPE_HEDERA_TOPIC_CREATE)
|
||||||
@ -92,6 +102,13 @@ namespace model
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool PendingTask::isHederaTransaction(TaskType type)
|
||||||
|
{
|
||||||
|
if (type && type >= TASK_TYPE_HEDERA_TOPIC_CREATE)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
Poco::Data::Statement PendingTask::_loadFromDB(Poco::Data::Session session, const std::string& fieldName)
|
Poco::Data::Statement PendingTask::_loadFromDB(Poco::Data::Session session, const std::string& fieldName)
|
||||||
{
|
{
|
||||||
Poco::Data::Statement select(session);
|
Poco::Data::Statement select(session);
|
||||||
@ -119,9 +136,9 @@ namespace model
|
|||||||
lock();
|
lock();
|
||||||
select << "SELECT id FROM " << getTableName()
|
select << "SELECT id FROM " << getTableName()
|
||||||
<< " WHERE user_id = ? "
|
<< " WHERE user_id = ? "
|
||||||
//<< " AND created LIKE ? "
|
<< " AND TIMESTAMPDIFF(SECOND, created, ?) = 0 "
|
||||||
<< " AND task_type_id = ? "
|
<< " AND task_type_id = ? "
|
||||||
, into(mID), use(mUserId), /*use(mCreated),*/ use(mTaskTypeId);
|
, into(mID), use(mUserId), use(mCreated), use(mTaskTypeId);
|
||||||
unlock();
|
unlock();
|
||||||
return select;
|
return select;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -56,6 +56,8 @@ namespace model {
|
|||||||
|
|
||||||
inline bool isGradidoTransaction() { SHARED_LOCK; return isGradidoTransaction((TaskType)mTaskTypeId); }
|
inline bool isGradidoTransaction() { SHARED_LOCK; return isGradidoTransaction((TaskType)mTaskTypeId); }
|
||||||
static bool isGradidoTransaction(TaskType type);
|
static bool isGradidoTransaction(TaskType type);
|
||||||
|
inline bool isHederaTransaction() { SHARED_LOCK; return isHederaTransaction((TaskType)mTaskTypeId); }
|
||||||
|
static bool isHederaTransaction(TaskType type);
|
||||||
|
|
||||||
static const char* typeToString(TaskType type);
|
static const char* typeToString(TaskType type);
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user