Problem with MySQL

Dearests,

I have a connect problem with MySQL.jl. I’m writing a module that do some custom queries to a local mysql database. It looks like:

module PfamDatabaseTools

global connection = MySQL.connect("127.0.0.1","myname","mypasswd")

include("file1.jl")
...
include("fileN.jl")

println(connection)
end #end module

Now I activate the package (]activate . ) and after precompilation I see correctly printed on screen
a regular connection


Host: 127.0.0.1
Port: 3306
User: myname
DB:

Now the problem is that if I now try to see what happens to my connection I get a

julia> PfamDatabaseTools.connection
Null MySQL Connection

I tryed to change global into const, but same result. Note that if I do the same outside the module by squeezing all file*jl into a single file, and opening the connection at the beginning of the file, everything works perfectly.

Shooting in the dark, I also tried to GC.@preserve the connection variable, with no success either. It’s like if at the end of the module precompilation, the connection disappears.

Any ideas?

Precompilation is done only once, if you want to connect to MySQL every time module is loaded, try putting the connection code into the special __init__() method. Even better approach would be to create connection only when you need it to be able to handle disconnections and similar things.

Thank @dfdx ! I added in the main module file a

function __init__()
    global connection = MySQL.connect("127.0.0.1","myname","mypasswd")
    nothing
end 

And everything works like a charm. I see that this solution is brittle as it is not able to deal with random disconnection. I’m thinking about your “even better approach”. Marking as solved!