Skip to content

Commit

Permalink
Fix prepared statement reconnect issue
Browse files Browse the repository at this point in the history
This commit fixes an issue where, if the connection between Logstash
and the database fails due to an attempt to use a closed prepared statement.
This results in subsequent attempts to use a prepared statement
failing when using scheduling, which can only be rectified by restarting Logstash.

This commit resets the `statement_prepared` flag, which means that the
prepared statement will be recreated upon the next scheduled run.

This commit also adds a backtrace to log entries when this issue occurs to provide
more visibility into the cause of the issue.
  • Loading branch information
robbavey committed Apr 7, 2020
1 parent a535465 commit 97a85ec
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/logstash/plugin_mixins/jdbc/jdbc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ def execute_statement
end
success = true
rescue Sequel::DatabaseConnectionError, Sequel::DatabaseError, Java::JavaSql::SQLException => e
@logger.warn("Exception when executing JDBC query", :exception => e)
details = { :exception => e.message }
details[:backtrace] = e.backtrace if @logger.debug?
@logger.warn("Exception when executing JDBC query", details)
else
@value_tracker.set_value(sql_last_value)
ensure
Expand Down
9 changes: 8 additions & 1 deletion lib/logstash/plugin_mixins/jdbc/statement_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ def build_query(db, sql_last_value)
end
bind_value_sql_last_value(sql_last_value)
statement_logger.log_statement_parameters(statement, parameters, nil)
db.call(name, parameters)
begin
db.call(name, parameters)
rescue => e
# clear the statement prepared flag - the statement may be closed by this
# time.
statement_prepared.make_false
raise e
end
end

def post_init(plugin)
Expand Down
1 change: 0 additions & 1 deletion spec/inputs/integration/integ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require "sequel"
require "sequel/adapters/jdbc"

# This test requires: Firebird installed to Mac OSX, it uses the built-in example database `employee`

describe LogStash::Inputs::Jdbc, :integration => true do
# This is a necessary change test-wide to guarantee that no local timezone
Expand Down

0 comments on commit 97a85ec

Please sign in to comment.