# How to share a duckdb database connection to many functions

**URL:** https://discourse.julialang.org/t/how-to-share-a-duckdb-database-connection-to-many-functions/102921
**Category:** New to Julia
**Tags:** modules, typed-globals, duckdb
**Created:** [August 17, 2023, 7:37pm UTC](https://discourse.julialang.org/t/how-to-share-a-duckdb-database-connection-to-many-functions/102921 "2023-08-17T19:37:25Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![rdavis120](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@rdavis120](https://discourse.julialang.org/u/rdavis120)
#### Post date: [August 17, 2023, 7:37pm UTC](https://discourse.julialang.org/t/how-to-share-a-duckdb-database-connection-to-many-functions/102921/1 "2023-08-17T19:37:25Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [August 19, 2023, 2:44pm UTC](https://discourse.julialang.org/t/how-to-share-a-duckdb-database-connection-to-many-functions/102921/2 "2023-08-19T14:44:49Z")

</div>

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.

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

```
