FredyH / MySQLOO

MySQLOO
GNU Lesser General Public License v2.1
140 stars 55 forks source link

Cannot use same PreparedStatement with different data multiple times in one Transaction #31

Closed meepen closed 4 years ago

meepen commented 5 years ago

It uses the last dataset given to the PrepareStatement

meepen commented 5 years ago

https://github.com/meepen/MySQLOO/commit/59a64e81120e62303f6d5a97557060c594cbc1b9

I fixed it here, not sure if right approach. Feel free to use.

FredyH commented 5 years ago

I am 95% sure this would leak memory, the reason I have a lua table to store the queries is exactly because tables us proper GC, while storing a reference does not.

Try running loads of transactions with callbacks that all reference all of the queries and the transaction itself (so that the closures keep references to them), and check mysqloo.objectCount() to see if anything leaks.

meepen commented 5 years ago

yeah it definitely does leak

meepen commented 5 years ago

actually both main and mine leak at same rate lol

FredyH commented 5 years ago

I did this test on my current version and it went back down from 50k objects to ~11 or something

local finished = 0;
local time = SysTime()
local query1 = db:prepare("SELECT ?")
local query2 = db:prepare("SELECT ?, 1")
local totalAmount = 50000
for i = 1, totalAmount do
    local transaction = db:createTransaction()
    query1:setNumber(1, math.random(1, 1000))
    query2:setNumber(1, math.random(1, 1000))
    transaction:addQuery(query1)
    transaction:addQuery(query2)
    function transaction:onSuccess()
        finished = finished + 1
        if (finished == totalAmount) then
            print("Done:", SysTime() - time, query1, query2, transaction)
            collectgarbage("collect")
        end
    end
    transaction:start()
end
FredyH commented 4 years ago

Fixed in master