diff --git a/lib/logstash/outputs/jdbc.rb b/lib/logstash/outputs/jdbc.rb index 4a04df6..198ed12 100644 --- a/lib/logstash/outputs/jdbc.rb +++ b/lib/logstash/outputs/jdbc.rb @@ -98,7 +98,7 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base end setup_and_test_pool! - + buffer_initialize( :max_items => @flush_size, :max_interval => @idle_flush_time, @@ -194,56 +194,55 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base end def safe_flush(events, teardown=false) - connection = @pool.getConnection() - statement = connection.prepareStatement(@statement[0]) - - events.each do |event| - next if event.cancelled? - next if @statement.length < 2 - statement = add_statement_event_params(statement, event) - - statement.addBatch() - end - + connection = nil + statement = nil begin + connection = @pool.getConnection() + statement = connection.prepareStatement(@statement[0]) + + events.each do |event| + next if event.cancelled? + next if @statement.length < 2 + statement = add_statement_event_params(statement, event) + + statement.addBatch() + end + statement.executeBatch() statement.close() @exceptions_tracker << nil - rescue => e - # Raising an exception will incur a retry from Stud::Buffer. - # Since the exceutebatch failed this should mean any events failed to be - # inserted will be re-run. We're going to log it for the lols anyway. log_jdbc_exception(e) ensure - connection.close(); + statement.close() unless statement.nil? + connection.close() unless connection.nil? end end def unsafe_flush(events, teardown=false) - connection = @pool.getConnection() + connection = nil + statement = nil + begin + connection = @pool.getConnection() - events.each do |event| - next if event.cancelled? - - statement = connection.prepareStatement(event.sprintf(@statement[0])) - statement = add_statement_event_params(statement, event) if @statement.length > 1 + events.each do |event| + next if event.cancelled? + + statement = connection.prepareStatement(event.sprintf(@statement[0])) + statement = add_statement_event_params(statement, event) if @statement.length > 1 - begin statement.execute() - + # cancel the event, since we may end up outputting the same event multiple times # if an exception happens later down the line event.cancel @exceptions_tracker << nil - rescue => e - # Raising an exception will incur a retry from Stud::Buffer. - # We log for the lols. - log_jdbc_exception(e) - ensure - statement.close() - connection.close() end + rescue => e + log_jdbc_exception(e) + ensure + statement.close() unless statement.nil? + connection.close() unless connection.nil? end end diff --git a/logstash-output-jdbc.gemspec b/logstash-output-jdbc.gemspec index 891b539..6cc55ba 100644 --- a/logstash-output-jdbc.gemspec +++ b/logstash-output-jdbc.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'logstash-output-jdbc' - s.version = "0.2.5" + s.version = "0.2.6-rc1" s.licenses = [ "Apache License (2.0)" ] s.summary = "This plugin allows you to output to SQL, via JDBC" s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program" diff --git a/spec/outputs/jdbc_spec.rb b/spec/outputs/jdbc_spec.rb index 48b599d..fd783b2 100644 --- a/spec/outputs/jdbc_spec.rb +++ b/spec/outputs/jdbc_spec.rb @@ -88,6 +88,7 @@ describe LogStash::Outputs::Jdbc do end stmt.close() c.close() + expect(count).to be > 0 end