Are database connection pools typically used with concurrency

I wouldn’t assume database connections are threadsafe (as the author of SQLite.jl and MySQL.jl, I know they aren’t; but I have been thinking of making them soon). I believe LibPQ.jl is last I checked.

I use the Pool structure in the ConcurrentUtilities.jl package (currently light on docs, but plentiful on docstrings: ConcurrentUtilities.jl/src/pools.jl at main · JuliaServices/ConcurrentUtilities.jl · GitHub). It’s fairly straightforward to use to set a max # of connections and then use an acquire/release pattern from various threads. I typically write a utility like the following to enforce proper usage:

function withconnection(f)
    conn = acquire(newconnection, POOL; isvalid=isopen)
    try
        return f(conn)
    finally
        release(POOL, conn)
    end
end

where newconnection is your own function that calls DBInterface.connect

2 Likes