-
Notifications
You must be signed in to change notification settings - Fork 64
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
Update how db connections are retrieved and released #802
Conversation
19cda6f
to
f51cb39
Compare
src/avram/database.cr
Outdated
@@ -153,7 +153,9 @@ abstract class Avram::Database | |||
|
|||
# :nodoc: | |||
def run | |||
yield current_connection || db | |||
yield current_connection |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this 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 😂
There was a problem hiding this 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
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 bewith_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.