I took me by surprise that
with DBInterface.execute(db, sql) as q
# do stuff with q
end
does not deterministically finalize (destruct) q
. After digging deeper (e.g. https://github.com/JuliaLang/julia/issues/7721 ) I found out, that up to mid-2019 it is a known problem in the Julia language specification.
There are exactly two walkarounds of this problem. One is to define
DBInterface.execute(f::Function, args...) = begin
conn = db_connect(args...)
try
f(conn)
finally
finalize(conn)
end
end
and use it like this:
DBInterface.execute(db, sql) do q
# do stuff with q
end
and other, low-level solution is
q = DBInterface.execute(db, sql)
try
# do stuff with q
finally
finalize(q)
end
The latter solution is a little tricky, because a slight variation will not work:
try q = DBInterface.execute(db, sql)
# do stuff with q
finally
finalize(q) # Error: q is out of scope here!
end
Stefan Karpinski was fond of a special suffix “!” notation:
with DBInterface.execute(db, sql)! as q
# do stuff with q
end
…but that thread is dead for over 6 months.
Did I miss any development on this topic?