oatpp / oatpp-postgresql

PostgreSQL adapter for oatpp ORM.
https://oatpp.io/
Apache License 2.0
18 stars 25 forks source link

[QUESTION] database macros thread-safe ? #28

Open MateusDornelles opened 6 months ago

MateusDornelles commented 6 months ago

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;
}