# Logstash JDBC Input PREPARE on every run

**URL:** <https://discuss.elastic.co/t/logstash-jdbc-input-prepare-on-every-run/375312>\
**Category:** Logstash\
**Tags:** jdbc\
**Created:** [March 3, 2025, 1:46pm UTC](https://discuss.elastic.co/t/logstash-jdbc-input-prepare-on-every-run/375312 "2025-03-03T13:46:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwjvaughan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dwjvaughan/32/10648_2.png) [@dwjvaughan](https://discuss.elastic.co/u/dwjvaughan)\
**Post date:** [March 3, 2025, 1:46pm UTC](https://discuss.elastic.co/t/logstash-jdbc-input-prepare-on-every-run/375312/1 "2025-03-03T13:46:51Z")

</div>

Hi,

Since upgrading from Logstash 8.7.1 to 8.17.2 I've noticed that the `PREPARE` statement is run every time (followed by the `EXECUTE`), where as previously `PREPARE` would run once at start up, and `EXECUTE` would run every minute. Source is Oracle 19c, and logstash is running using the official container.

Here's one of my inputs, but this is happening on all where `use_prepared_statements` is `true`:

```auto
  jdbc {
    jdbc_driver_library => '/opt/logstash/config/jdbc/ojdbc11.jar'
    jdbc_driver_class => 'Java::oracle.jdbc.driver.OracleDriver'
    jdbc_connection_string => "jdbc:oracle:thin:@${DB_HOSTNAME}:${DB_PORT}:${DB_SID}"
    jdbc_user => "${DB_USER}"
    jdbc_password => "${DB_PASSWORD}"
    schedule => '* * * * *'
    last_run_metadata_path => '/opt/logstash/lastrun/my_query'
    statement_filepath => '/opt/logstash/sql/my_query.sql'
    prepared_statement_name => "my_query"
    use_prepared_statements => true
    add_field => {
      ...some tags
    } 
  }

```

This feels like a bug, but if I'm doing something wrong, I'd love to know.

Thanks,  
Dave

---

<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:** [March 3, 2025, 3:41pm UTC](https://discuss.elastic.co/t/logstash-jdbc-input-prepare-on-every-run/375312/2 "2025-03-03T15:41:20Z")

</div>

> [@dwjvaughan](#):
>
> Since upgrading from Logstash 8.7.1 to 8.17.2 I've noticed that the `PREPARE` statement is run every time (followed by the `EXECUTE`), where as previously `PREPARE` would run once at start up, and `EXECUTE` would run every minute.

I haven't been through it in detail, but I suspect it is related to [this](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/130) PR. Basically, it creates a new database connection for every scheduled execution, and it does not surprise me in the least that that would require doing the prepare again.

I don't think you can opt out. The prepared statement and the connection are linked. If one is replaced the other has to be.

Hopefully your database does prepared statement caching server-side.

---

<div class="post-metadata">

**Author:** ![dwjvaughan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dwjvaughan/32/10648_2.png) [@dwjvaughan](https://discuss.elastic.co/u/dwjvaughan)\
**Post date:** [March 3, 2025, 3:56pm UTC](https://discuss.elastic.co/t/logstash-jdbc-input-prepare-on-every-run/375312/3 "2025-03-03T15:56:41Z")

</div>

Thanks @Badger - I see the change was to resolve another issue so it makes sense. The `PREPARE` statements appear to be cached as the execution time is _very_ small. Appreciate you looking into it.
