Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jdbc_fetch_size usage with postgresql #368

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/logstash/plugin_mixins/jdbc/jdbc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def open_jdbc_connection
require "java"
require "sequel"
require "sequel/adapters/jdbc"
require "sequel/adapters/jdbc/transactions"

Sequel.application_timezone = @plugin_timezone.to_sym
if @drivers_loaded.false?
Expand All @@ -183,6 +184,7 @@ def open_jdbc_connection
end
@database = jdbc_connect()
@database.extension(:pagination)
@database.extend(Sequel::JDBC::Transactions)
if @jdbc_default_timezone
@database.extension(:named_timezones)
@database.timezone = @jdbc_default_timezone
Expand Down
18 changes: 11 additions & 7 deletions lib/logstash/plugin_mixins/jdbc/statement_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ class NormalStatementHandler < StatementHandler
# @yieldparam row [Hash{Symbol=>Object}]
def perform_query(db, sql_last_value, jdbc_paging_enabled, jdbc_page_size)
query = build_query(db, sql_last_value)
if jdbc_paging_enabled
query.each_page(jdbc_page_size) do |paged_dataset|
paged_dataset.each do |row|
# Execute query in transaction cause PG driver require autocommit off for set fetch count
# See: https://jdbc.postgresql.org/documentation/head/query.html
db.transaction(rollback: :always) do
if jdbc_paging_enabled
query.each_page(jdbc_page_size) do |paged_dataset|
paged_dataset.each do |row|
yield row
end
end
else
query.each do |row|
yield row
end
end
else
query.each do |row|
yield row
end
end
end

Expand Down
3 changes: 1 addition & 2 deletions spec/inputs/jdbc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,6 @@
{
"statement" => "SELECT * FROM test_table",
"jdbc_pool_timeout" => 0,
"jdbc_connection_string" => 'mock://localhost:1527/db',
"sequel_opts" => {
"max_connections" => 1
}
Expand Down Expand Up @@ -1026,7 +1025,7 @@
end

it "should report the statements to logging" do
expect(plugin.logger).to receive(:debug).once
expect(plugin.logger).to receive(:debug).thrice
plugin.run(queue)
end
end
Expand Down