Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] database macros thread-safe ? #28

Open
MateusDornelles opened this issue Mar 17, 2024 · 0 comments
Open

[QUESTION] database macros thread-safe ? #28

MateusDornelles opened this issue Mar 17, 2024 · 0 comments

Comments

@MateusDornelles
Copy link

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;
}
@MateusDornelles MateusDornelles changed the title [QUESTION] [QUESTION] database macros thread-safe ? Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant