# Logstash JDBC Inputs SQL Connections are never closed

**URL:** <https://discuss.elastic.co/t/logstash-jdbc-inputs-sql-connections-are-never-closed/366160>\
**Category:** Logstash\
**Created:** [September 6, 2024, 2:47pm UTC](https://discuss.elastic.co/t/logstash-jdbc-inputs-sql-connections-are-never-closed/366160 "2024-09-06T14:47:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lkouts](https://avatars.discourse-cdn.com/v4/letter/l/8e8cbc/32.png) [@lkouts](https://discuss.elastic.co/u/lkouts)\
**Post date:** [September 6, 2024, 2:47pm UTC](https://discuss.elastic.co/t/logstash-jdbc-inputs-sql-connections-are-never-closed/366160/1 "2024-09-06T14:47:44Z")

</div>

Hey Folks,  
I have a logstash configuration that has 10 jdbc inputs connecting to the same SQL Server but to diffrent databases. Scheduler is set to run every 2 minutes. All works correctly however the SQL Connections are never closed.

```auto
My logstash pipeline config example(partial)
        input {
          jdbc {
            jdbc_driver_library => "/usr/share/logstash/jars/mssql-jdbc-12.6.1.jre8.jar"
            jdbc_driver_class => "com.microsoft.sqlserver.jdbc.SQLServerDriver"
            jdbc_connection_string => "jdbc:sqlserver:// ******:1433;databaseName=****** 1;trustservercertificate=true"
            jdbc_user => "${ **** }"
            jdbc_password => "${ **** }"     
            tracking_column => "unix_ts_in_secs"
            use_column_value => true
            tracking_column_type => "numeric"
            schedule => " */2 * * * *"
            add_field => { "source_index" => "logstash-idx_index1" }
            last_run_metadata_path => "/usr/share/logstash/data/index1"
            statement => "${OBJECTINSTANCE_QUERY}"
          }}
        input {
          jdbc {
            jdbc_driver_library => "/usr/share/logstash/jars/mssql-jdbc-12.6.1.jre8.jar"
            jdbc_driver_class => "com.microsoft.sqlserver.jdbc.SQLServerDriver"
            jdbc_connection_string => "jdbc:sqlserver:// ******:1433;databaseName=****** 2;trustservercertificate=true"
            jdbc_user => "${ **** }"
            jdbc_password => "${ **** }"     
            jdbc_paging_enabled => true
            jdbc_pool_timeout => 60
            tracking_column => "unix_ts_in_secs"
            use_column_value => true
            tracking_column_type => "numeric"
            schedule => " */2 * * * *"
            add_field => { "source_index" => "logstash-idx_index1" }
            last_run_metadata_path => "/usr/share/logstash/data/index2"
            statement => "${OBJECTINSTANCE_QUERY}"
          }}

```

Any suggestions? I have tried various timeout props and sequel\_opts without any success.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [September 6, 2024, 4:34pm UTC](https://discuss.elastic.co/t/logstash-jdbc-inputs-sql-connections-are-never-closed/366160/2 "2024-09-06T16:34:31Z")

</div>

> [@lkouts](#):
>
> All works correctly however the SQL Connections are never closed.

That is expected. The input opens up the connection pool when [register](https://github.com/logstash-plugins/logstash-integration-jdbc/blob/2c84d5fa0d9787e8e444ca7d6fed729cbb7b59ec/lib/logstash/inputs/jdbc.rb#L241) is called (i.e. when the pipeline is being initialized) and keeps reusing them. It does not close them unless the pipeline is [stopped](https://github.com/logstash-plugins/logstash-integration-jdbc/blob/2c84d5fa0d9787e8e444ca7d6fed729cbb7b59ec/lib/logstash/inputs/jdbc.rb#L341).

If the DB closes long-lived connections then you can [validate them](https://www.elastic.co/guide/en/logstash/current/plugins-inputs-jdbc.html#_unable_to_reuse_connections) before use.

---

<div class="post-metadata">

**Author:** ![lkouts](https://avatars.discourse-cdn.com/v4/letter/l/8e8cbc/32.png) [@lkouts](https://discuss.elastic.co/u/lkouts)\
**Post date:** [September 6, 2024, 5:52pm UTC](https://discuss.elastic.co/t/logstash-jdbc-inputs-sql-connections-are-never-closed/366160/3 "2024-09-06T17:52:38Z")

</div>

My logstash may ultimately have over 200 inputs to different databases of the same schema on the same SQL Server. Some may have have slightly cron schedules. Keeping 200+ connections open for this sounds expensive.  
It also seems that each input has it's own set of connection pools. Is there any capability of reusing\sharing the same connection pool that spans multiple inputs?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [September 6, 2024, 7:20pm UTC](https://discuss.elastic.co/t/logstash-jdbc-inputs-sql-connections-are-never-closed/366160/4 "2024-09-06T19:20:05Z")

</div>

> [@lkouts](#):
>
> Is there any capability of reusing\sharing the same connection pool that spans multiple inputs?

I do not think so, but adjusting the size of the connection pool is explicitly called out in the documentation of [sequel\_opts option](https://github.com/logstash-plugins/logstash-integration-jdbc/blob/2c84d5fa0d9787e8e444ca7d6fed729cbb7b59ec/lib/logstash/plugin_mixins/jdbc/jdbc.rb#L91), so you may be able to reduce the number of connections. The default size is 4.
