# How to configure and pass db connection when using Puma

**URL:** <https://discourse.rom-rb.org/t/how-to-configure-and-pass-db-connection-when-using-puma/621>\
**Category:** Issues\
**Created:** [July 14, 2023, 5:35pm UTC](https://discourse.rom-rb.org/t/how-to-configure-and-pass-db-connection-when-using-puma/621 "2023-07-14T17:35:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![codeslinger](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@codeslinger](https://discourse.rom-rb.org/u/codeslinger)\
**Post date:** [July 14, 2023, 5:35pm UTC](https://discourse.rom-rb.org/t/how-to-configure-and-pass-db-connection-when-using-puma/621/1 "2023-07-14T17:35:04Z")

</div>

Hi,  
We have puma configured with 4 workers and min/max threads set to 5. We currently create a rom configuration and cache it in a variable.

```auto
module MyPersistance
  extend Dry::Configurable
   ...
   def self.configuration
      @configuration ||= ROM::Configuration.new(gateways) do |config|
         ...
         config.auto_registration(registration_path, ...)
         ...

```

And do the same for a container.

```auto
def self.container
   @container ||= ROM.container(configuration)

```

Then when instantiating our repositories we pass the container to #new.

```auto
MyRepository.new(MyPersistance.container, ...)

```

This is obviously not thread safe and is causing lots of errors not seen using webrick.  
If I remove the caching I get the error “Mysql2::Error: Too many connections”

I have read this post and related issue:  
[rom-gateways-and-sequel-pools](https://discourse.rom-rb.org/t/rom-gateways-and-sequel-pools/377)

I need some more concrete guidance. What is your recommendation on structuring this correctly to work with puma?

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/alassek/32/260_2.png) [@alassek](https://discourse.rom-rb.org/u/alassek)\
**Post date:** [July 19, 2023, 6:11pm UTC](https://discourse.rom-rb.org/t/how-to-configure-and-pass-db-connection-when-using-puma/621/2 "2023-07-19T18:11:21Z")

</div>

My configuration looks something like this

```ruby
    # The default options for ROM::Gateway
    def connection_options
      {
        connect_timeout: 2,
        max_connections: @slice.settings.max_threads,
        extensions: %i[connection_validator error_sql pg_array pg_json ksuid]
      }
    end

    # @api private
    def to_gateway
      ROM::Gateway.setup(:sql, to_uri, connection_options)
    end

    # @api private
    def to_config
      ROM::Configuration.new(default: to_gateway).tap do |config|
        config.gateways.each do |(_, gw)|
          gw.connection.pool.connection_validation_timeout = -1
        end

        config.plugin(:sql, relations: :auto_restrictions)
      end
    end

```

Where `@slice.settings.max_threads` comes from and env var, and is set in the puma config:

```auto
max_threads_count = ENV.fetch("HANAMI_MAX_THREADS", 5)
min_threads_count = ENV.fetch("HANAMI_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count

```

---

<div class="post-metadata">

**Author:** ![cofemei](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/cofemei/32/304_2.png) [@cofemei](https://discourse.rom-rb.org/u/cofemei)\
**Post date:** [July 29, 2023, 12:36pm UTC](https://discourse.rom-rb.org/t/how-to-configure-and-pass-db-connection-when-using-puma/621/3 "2023-07-29T12:36:12Z")

</div>

You might need up to 20 connections for 4 workers and 5 threads. If your Configuration connection is over 20, try setting it to be more than 20.

Another possible issue might be that you haven’t properly released the database connection. If this is the case, you might notice many unhandled exceptions.
