lib/logstash/outputs/charrington/alter_table.rb in logstash-output-charrington-0.2.1 vs lib/logstash/outputs/charrington/alter_table.rb in logstash-output-charrington-0.2.2
- old
+ new
@@ -64,15 +64,17 @@
end
end
def current_table_columns
sql = "SELECT * FROM #{table_name} LIMIT 1;"
- rs = executeQuery(sql)
+ stmt, rs = executeQuery(prep_sql(sql))
meta_data = rs.getMetaData()
+ stmt.close unless stmt.nil?
column_count = meta_data.getColumnCount()
-
(1..column_count).map {|i| meta_data.getColumnName(i) }
+ ensure
+ stmt.close unless stmt.nil?
end
def execute(sql)
stmt = connection.prepareStatement(prep_sql(sql))
stmt.execute()
@@ -82,13 +84,19 @@
stmt.close unless stmt.nil?
end
def executeQuery(sql)
stmt = connection.createStatement()
- stmt.executeQuery(prep_sql(sql))
+ # only close the statement if something goes wrong
+ # otherwise, the caller is responsible for closing the
+ # statement when they are doen with the result set
+ return stmt, stmt.executeQuery(prep_sql(sql))
rescue Java::OrgPostgresqlUtil::PSQLException => e
+ puts "PSQLException: #{e.message}"
+ stmt.close unless stmt.nil?
# @logger.error("#{e.message}")
- ensure
+ rescue => e
+ puts "Unknown exception: #{e.message}"
stmt.close unless stmt.nil?
end
def prep_sql(sql)
sql.gsub(/\s+/, " ").strip