Skip to content

Commit

Permalink
fix: database disconnect error when service not accessed frequently
Browse files Browse the repository at this point in the history
bethesque committed Oct 5, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent cc6666b commit d146382
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion pact_broker/database_connection.rb
Original file line number Diff line number Diff line change
@@ -16,5 +16,25 @@ def create_database_connection(logger)
credentials[:port] = ENV['PACT_BROKER_DATABASE_PORT'].to_i
end

Sequel.connect(credentials.merge(logger: DatabaseLogger.new(logger), encoding: 'utf8'))
##
# Sequel by default does not test connections in its connection pool before
# handing them to a client. To enable connection testing you need to load the
# "connection_validator" extension like below. The connection validator
# extension is configurable, by default it only checks connections once per
# hour:
#
# http://sequel.rubyforge.org/rdoc-plugins/files/lib/sequel/extensions/connection_validator_rb.html
#
#
# A gotcha here is that it is not enough to enable the "connection_validator"
# extension, we also need to specify that we want to use the threaded connection
# pool, as noted in the documentation for the extension.
#
# -1 means that connections will be validated every time, which avoids errors
# when databases are restarted and connections are killed. This has a performance
# penalty, so consider increasing this timeout if building a frequently accessed service.
connection = Sequel.connect(credentials.merge(logger: DatabaseLogger.new(logger), encoding: 'utf8'))
connection.extension(:connection_validator)
connection.pool.connection_validation_timeout = -1
connection
end

0 comments on commit d146382

Please sign in to comment.