Open mcmcgrath13 opened 4 years ago
Hey @mcmcgrath13! Thanks for opening this issue. It's funny because I've definitely been thinking about this over the last little while.
I'd love to see efforts here and am happy to chat on slack or in this issue about design ideas; just let me know what you're thinking!
I'm going to start playing around w/ prototyping a generic ConnectionPooling.jl package.
might be useful:
DataFrames, MySQL, Dates
# ]add MySQL@1.1.2
addslashes(x) = x
function addslashes(x::String)
x = replace(x,r"\\+(['\"\\])"=>s"\1")
x = replace(x,r"(['\"\\])"=>s"\\\1")
end
addslashes(x::T) where T <: Real = x
addslashes(x::Array) = json(x)
addslashes(x::DateTime) = Dates.format(x,"yyyy-mm-dd HH:MM:SS")
MYSQL_NUM_CONNECTIONS = 10
# Mysql连接
function mysql_init()
mysql_conn = MySQL.DBInterface.connect(MySQL.Connection, serv.address, serv.user, serv.pass; db=serv.db, port=serv.port, reconnect=true)
MySQL.DBInterface.execute(mysql_conn,"set names utf8")
mysql_conn
end
mysql_conn_pool = [ mysql_init() for i in 1:MYSQL_NUM_CONNECTIONS ]
mysql_conn = mysql_conn_pool[1]
function mysql_query(sql::String)
tmp_conn = mysql_conn_pool[rand(1:MYSQL_NUM_CONNECTIONS)]
try
MySQL.DBInterface.execute(tmp_conn,sql) |> DataFrame
catch e
println()
@show tmp_conn
@show e
@warn sql
return nothing
end
end
I don't think you want pooling to be done randomly at the query level. Because things like last_insert_id()
will fail. right?
@quinnj I'm thinking about building out some infrastructure for pooling connections (similar to the js mysql package). A few questions: