Skip to content

Commit bb27219

Browse files
authored
Fix prepared statement reconnect issue (#25)
* Fix prepared statement reconnect issue 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.
1 parent df09843 commit bb27219

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
## 5.0.3
2+
- Fixed issue where a lost connection to the database can cause errors when using prepared statements with the scheduler [#25](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/25)
3+
14
## 5.0.2
25
- Fixed potential resource leak by ensuring scheduler is shutdown when a pipeline encounter an error during the running [#28](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/28)
3-
6+
47
## 5.0.1
58
- Fixed tracking_column regression with Postgresql Numeric types [#17](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/17)
69
- Fixed driver loading when file not accessible [#15](https://github.com/logstash-plugins/logstash-integration-jdbc/pull/15)

lib/logstash/plugin_mixins/jdbc/jdbc.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ def execute_statement
260260
end
261261
success = true
262262
rescue Sequel::DatabaseConnectionError, Sequel::DatabaseError, Java::JavaSql::SQLException => e
263-
@logger.warn("Exception when executing JDBC query", :exception => e)
263+
details = { :exception => e.message }
264+
details[:backtrace] = e.backtrace if @logger.debug?
265+
@logger.warn("Exception when executing JDBC query", details)
264266
else
265267
@value_tracker.set_value(sql_last_value)
266268
ensure

lib/logstash/plugin_mixins/jdbc/statement_handler.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ def build_query(db, sql_last_value)
9797
end
9898
bind_value_sql_last_value(sql_last_value)
9999
statement_logger.log_statement_parameters(statement, parameters, nil)
100-
db.call(name, parameters)
100+
begin
101+
db.call(name, parameters)
102+
rescue => e
103+
# clear the statement prepared flag - the statement may be closed by this
104+
# time.
105+
statement_prepared.make_false
106+
raise e
107+
end
101108
end
102109

103110
def post_init(plugin)

logstash-integration-jdbc.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'logstash-integration-jdbc'
3-
s.version = '5.0.2'
3+
s.version = '5.0.3'
44
s.licenses = ['Apache License (2.0)']
55
s.summary = "Integration with JDBC - input and filter plugins"
66
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"

spec/inputs/integration/integ_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require "sequel"
44
require "sequel/adapters/jdbc"
55

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

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

0 commit comments

Comments
 (0)