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

Update how db connections are retrieved and released #802

Merged

Conversation

matthewmcgarvey
Copy link
Member

@matthewmcgarvey matthewmcgarvey commented Jan 26, 2022

Fixes #799

In #800 we already fixed one place where we weren't releasing connections correctly. We found another place where queries were not releasing connections since they don't use transactions. Instead of add the connection release code in two places, I updated current_connection to be with_connection that takes a block. When the block finishes (error or not) it will always attempt to release the connection back to the pool. If there is an open transaction, it won't do it.

@matthewmcgarvey matthewmcgarvey force-pushed the query-connection-release branch from 19cda6f to f51cb39 Compare January 26, 2022 05:36
@@ -153,7 +153,9 @@ abstract class Avram::Database

# :nodoc:
def run
yield current_connection || db
yield current_connection
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The || db was unnecessary as current_connection checks out a connection if there isn't already one

@@ -62,6 +62,12 @@ describe Avram::Queryable do
query.statement.should eq "SELECT #{User::COLUMN_SQL} FROM users WHERE users.age >= $1 AND users.name = $2"
end

it "releases connection if no open transaction", tags: Avram::SpecHelper::TRUNCATE do
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly like #800 we use truncation to avoid having a transaction on the connection by default

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah interesting! Yeah, this DB stuff is tricky 😂

@matthewmcgarvey matthewmcgarvey changed the title Try releasing connection after Database#run Update how db connections are retrieved and released Jan 26, 2022
Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooo I like that better. Just having the release in a single place makes that much easier to read

@matthewmcgarvey matthewmcgarvey merged commit e942cb9 into luckyframework:master Jan 26, 2022
@matthewmcgarvey matthewmcgarvey deleted the query-connection-release branch January 26, 2022 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Latest Avram main results in "FATAL: sorry, too many clients already"
3 participants