Provisionally address issue 46 for v5

This commit is contained in:
Karl Southern 2016-07-07 08:53:49 +01:00
parent 6c852d21dc
commit 34708157f4

View File

@ -246,10 +246,18 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
def add_statement_event_params(statement, event) def add_statement_event_params(statement, event)
@statement[1..-1].each_with_index do |i, idx| @statement[1..-1].each_with_index do |i, idx|
case event.get(i) value = event[i]
value = if value.nil? and i.to_s =~ /%{/
event.sprintf(i)
else
value
end
case value
when Time when Time
# See LogStash::Timestamp, below, for the why behind strftime. # See LogStash::Timestamp, below, for the why behind strftime.
statement.setString(idx + 1, event.get(i).strftime(STRFTIME_FMT)) statement.setString(idx + 1, value.strftime(STRFTIME_FMT))
when LogStash::Timestamp when LogStash::Timestamp
# XXX: Using setString as opposed to setTimestamp, because setTimestamp # XXX: Using setString as opposed to setTimestamp, because setTimestamp
# doesn't behave correctly in some drivers (Known: sqlite) # doesn't behave correctly in some drivers (Known: sqlite)
@ -258,29 +266,25 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
# choke on the 'T' in the string (Known: Derby). # choke on the 'T' in the string (Known: Derby).
# #
# strftime appears to be the most reliable across drivers. # strftime appears to be the most reliable across drivers.
statement.setString(idx + 1, event.get(i).time.strftime(STRFTIME_FMT)) statement.setString(idx + 1, value.time.strftime(STRFTIME_FMT))
when Fixnum, Integer when Fixnum, Integer
statement.setInt(idx + 1, event.get(i)) statement.setInt(idx + 1, value)
when Float when Float
statement.setFloat(idx + 1, event.get(i)) statement.setFloat(idx + 1, value)
when String when String
statement.setString(idx + 1, event.get(i)) statement.setString(idx + 1, value)
when true, false when true, false
statement.setBoolean(idx + 1, event.get(i)) statement.setBoolean(idx + 1, value)
else
if event.get(i).nil? && i =~ /%\{/
statement.setString(idx + 1, event.sprintf(i))
else else
statement.setString(idx + 1, nil) statement.setString(idx + 1, nil)
end end
end end
end
statement statement
end end
def retry_exception?(exception) def retry_exception?(exception)
retrying = (exception.respond_to? 'getSQLState' and (RETRYABLE_SQLSTATE_CLASSES.include?(exception.getSQLState.to_s[0,2]) or @retry_sql_states.include?(exception.getSQLState))) retrying = (exception.respond_to? 'getSQLState' and (RETRYABLE_SQLSTATE_CLASSES.include?(exception.getSQLState[0,2]) or @retry_sql_states.include?(exception.getSQLState)))
log_jdbc_exception(exception, retrying) log_jdbc_exception(exception, retrying)
retrying retrying
@ -288,14 +292,18 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base
def log_jdbc_exception(exception, retrying) def log_jdbc_exception(exception, retrying)
current_exception = exception current_exception = exception
log_text = 'JDBC Exception. ' + (retrying ? 'Retrying' : 'No retry') + '.'
loop do loop do
if retrying @logger.error(log_text, :exception => current_exception, :backtrace => current_exception.backtrace)
@logger.warn('JDBC - Exception. Retrying.', exception: current_exception)
if current_exception.respond_to? 'getNextException'
current_exception = current_exception.getNextException()
else else
@logger.error('JDBC - Exception. Not retrying. Dropping event.', exception: current_exception) current_exception = nil
end end
current_exception = current_exception.getNextException
break if current_exception.nil? break if current_exception == nil
end end
end end