Skip to content

Commit 56a4e24

Browse files
committed
Correctly close driver after a healthcheck failure
Fixes #227 . Fixes #228
1 parent 192fc14 commit 56a4e24

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 4.2.1
2+
- Fix bug where failed healthchecks would not call a non-existant method and suppress the real error
3+
14
## 4.2.0
25
- Automatically reconnect on connection issues
36
- Fix test failures

lib/logstash/plugin_mixins/jdbc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def open_jdbc_connection
171171
@database.test_connection
172172
rescue Sequel::DatabaseConnectionError => e
173173
@logger.warn("Failed test_connection.")
174-
@database.close_jdbc_connection
174+
close_jdbc_connection
175175

176176
#TODO return false and let the plugin raise a LogStash::ConfigurationError
177177
raise e

logstash-input-jdbc.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'logstash-input-jdbc'
3-
s.version = '4.2.1'
3+
s.version = '4.2.2'
44
s.licenses = ['Apache License (2.0)']
55
s.summary = "This example input streams a string at a definable interval."
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"
@@ -28,4 +28,5 @@ Gem::Specification.new do |s|
2828
s.add_development_dependency 'logstash-devutils'
2929
s.add_development_dependency 'timecop'
3030
s.add_development_dependency 'jdbc-derby'
31+
s.add_development_dependency 'jdbc-mysql'
3132
end

spec/inputs/jdbc_spec.rb

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
require "logstash/devutils/rspec/spec_helper"
33
require "logstash/inputs/jdbc"
44
require "jdbc/derby"
5+
require "jdbc/mysql"
6+
Jdbc::MySQL.load_driver
57
require "sequel"
68
require "sequel/adapters/jdbc"
79
require "timecop"
@@ -26,19 +28,24 @@
2628
end
2729

2830
before :each do
29-
Jdbc::Derby.load_driver
30-
db.create_table :test_table do
31-
DateTime :created_at
32-
Integer :num
33-
String :string
34-
DateTime :custom_time
35-
end
36-
db << "CREATE TABLE types_table (num INTEGER, string VARCHAR(255), started_at DATE, custom_time TIMESTAMP, ranking DECIMAL(16,6))"
31+
if !RSpec.current_example.metadata[:no_connection]
32+
# before body
33+
Jdbc::Derby.load_driver
34+
db.create_table :test_table do
35+
DateTime :created_at
36+
Integer :num
37+
String :string
38+
DateTime :custom_time
39+
end
40+
db << "CREATE TABLE types_table (num INTEGER, string VARCHAR(255), started_at DATE, custom_time TIMESTAMP, ranking DECIMAL(16,6))"
41+
end
3742
end
3843

3944
after :each do
40-
db.drop_table(:test_table)
41-
db.drop_table(:types_table)
45+
if !RSpec.current_example.metadata[:no_connection]
46+
db.drop_table(:test_table)
47+
db.drop_table(:types_table)
48+
end
4249
end
4350

4451
context "when registering and tearing down" do
@@ -87,6 +94,24 @@
8794
end
8895
end
8996

97+
context "when connecting to a non-existent server", :no_connection => true do
98+
let(:mixin_settings) do
99+
super.merge(
100+
"jdbc_driver_class" => "com.mysql.jdbc.Driver",
101+
"jdbc_connection_string" => "jdbc:mysql://localhost:99999/somedb"
102+
)
103+
end
104+
let(:settings) { super.merge("statement" => "SELECT 1 as col1 FROM test_table", "jdbc_user" => "foo", "jdbc_password" => "bar") }
105+
106+
it "should not register correctly" do
107+
plugin.register
108+
q = Queue.new
109+
expect do
110+
plugin.run(q)
111+
end.to raise_error(::Sequel::DatabaseConnectionError)
112+
end
113+
end
114+
90115
context "when both jdbc_password and jdbc_password_filepath arguments are passed" do
91116
let(:statement) { "SELECT * from test_table" }
92117
let(:jdbc_password) { "secret" }

0 commit comments

Comments
 (0)