How to share a duckdb database connection to many functions

What is the best practice in Julia to share the a database connection to several functions in a module? Such as a connection from the DBInterface package.

Especially, without exporting the connection variable itself, so that the exported functions do not expose the connection? The database connection would not be a constant because it would be initialized at run time.

You could consider a global with a concrete type binding or a global constant Ref.

It’s probably a better idea to pack this into a struct and pass it around for thread safety and scalability, defaulting to an global as above for ease of use.

Another idea is to generate closures that contain the connection.

function generate_connection_closure(conn::DbConnection)
    function read_data()
          # use conn here
    end
end