Skip to content

[QUESTION] database macros thread-safe ?  #28

Description

@MateusDornelles

Hi, i have an endpoint that calls the bellow function, if a don't use the mutex, the service stops, all subsequent calls to the database freezes (the rest works). If i do multiple calls to the database inside the same methods they are not thread-safe ?
I tested using Apache benchmark using two connections (ab -n 300 -c2 .....).

oatpp::Object<AdminRoomsProgrammingDto> AdminServices::getRoomProgrammingById(const oatpp::UInt32 &whitelabelid,
                                                                              const oatpp::UInt32 &roomid,
                                                                              const oatpp::UInt32 &id, int &error)
{
    std::lock_guard<std::mutex> lock(dbMutex); <==
    auto programming = oatpp::Object<AdminRoomsProgrammingDto>::createShared();    
    error = 0;
    try {        
        auto dbResult = m_database->getRoomProgrammingById(whitelabelid, roomid, id);
        if (dbResult != nullptr && dbResult->isSuccess())
        {            
            auto items = dbResult->fetch<oatpp::Vector<oatpp::Object<AdminRoomsProgrammingDto>>>();
            if (items != nullptr && !items->empty())
            {
                programming = items[0];                
                auto dbItem = m_database->getAdminRoomProgrammingDetail(whitelabelid, roomid, programming->programmingid);
                if (dbItem->isSuccess())
                {
                    auto dbItems = dbItem->fetch<oatpp::Vector<oatpp::Object<AdminRoomDetailDto>>>();
                    programming->matchprizes = dbItems;
                }
                auto dbAc = m_database->getRoomProgrammingAc(whitelabelid, roomid, programming->programmingid);
                if (dbAc->isSuccess())
                {                    
                    auto dbAcItems = dbAc->fetch<oatpp::Vector<oatpp::Object<AcItemDto>>>();
                    if (dbAcItems->size() > 0)
                    {
                        programming->acitem = dbAcItems[0];
                    }
                }
            }
        } else {
            error = -2;
        }
    } catch (const std::exception &e) {
        error = -1;
        OATPP_LOGD("SERVICES", "%s", e.what());
    } catch (...) {
        error = -1;
        OATPP_LOGD("SERVICES","Unknow exception");
    }
    return programming;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions